OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA][resttemplate library] - ApiClientHttpRequestInterceptor logResponse() interferes with response streams that are ByteArrayOutputStream (and probably other types of stream)

Open
#9,315 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The generated class foo.bar.ApiClient.ApiClientHttpRequestInterceptor calls a logResponse() method before returning the response to the RestTemplate.

This leaves the response body stream position at the end of the stream.

When using in conjunction with the Spring org.springframework.test.web.client.MockRestServiceServer, in a unit test, the response is always considered as empty, by the RestTemplate.

In this case, the response is of class org.springframework.mock.http.client.MockClientHttpResponse.
The body stream is of class java.io.ByteArrayInputStream.ByteArrayInputStream(byte[])

Eventually, the RestTemplate will call org.springframework.web.client.MessageBodyClientHttpResponseWrapper.org.springframework.web.client.MessageBodyClientHttpResponseWrapper.hasEmptyMessageBody() to determine if there is any content in the body to process.

This will always return false, because it (basically) calls read() on the response body stream, which returns -1, because we are already positioned at the end of the stream.

openapi-generator version

5.0.1 and 5.1.1

OpenAPI declaration file content or url

N/A

Generation Details
		<plugin>
		    <groupId>org.openapitools</groupId>
		    <artifactId>openapi-generator-maven-plugin</artifactId>
		    <executions>
		        <execution>
		        	<phase>generate-sources</phase>
		            <goals>
		                <goal>generate</goal>
		            </goals>
		            <configuration>
		                <inputSpec>${outbound.api}</inputSpec>
		                <output>${project.build.directory}/generated-sources/swagger</output>
		                <generatorName>java</generatorName>
		                <addCompileSourceRoot>false</addCompileSourceRoot>
		                <apiPackage>com.kirona.jm.purchasing.outbound.client</apiPackage>
		                <modelPackage>com.kirona.jm.purchasing.outbound.client.model</modelPackage>
		                <artifactId>${project.artifactId}</artifactId>
		                <groupId>${project.groupId}</groupId>
		                <configHelp>false</configHelp>
		                <configOptions>
		                	<java8>true</java8>
		                	<dateLibrary>java8</dateLibrary>
		                </configOptions>
		                <generateApis>true</generateApis>
		                <generateModels>true</generateModels>
		                <generateModelTests>false</generateModelTests>
		                <generateSupportingFiles>true</generateSupportingFiles>
		                <generateApiTests>false</generateApiTests>
		                <library>resttemplate</library>
		            </configuration>
		        </execution>
		    </executions>
		</plugin> 
Steps to reproduce

com.kirona.jm.purchasing.restapis.zip

I've attached a Maven multi module project.
Extract it, and run a 'mvn clean package' in the folder com.kirona.jm.purchasing.restapis\outbound-client
Unit test should fail -
Failed tests: testSubmitRequisition(com.kirona.jm.purchasing.outbound.client.TestClientNoAuth)

Related issues/PRs
Suggest a fix

Remove the logging (probably of the request AND the response) in ApiClientHttpRequestInterceptor.
You can log the request and response in other ways, in Spring.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the Java resttemplate library source or template that generates ApiClientHttpRequestInterceptor and inspect logResponse(). Reproduce the failure with the attached Maven project using mvn clean package in outbound-client. Done means the MockRestServiceServer response body remains readable by RestTemplate and the failing test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.