OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA] Generated DefaultApi.java with native library uses a non-markable (HttpResponse)InputStream
Nobody has claimed this yet.
- 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
There is no possibility to log the resopnse body, based on a bug:
The generated DefaultApi response interceptor (which is useful ie. for logging),
could not be used, because the body content as InputStream could be read at only once,
so if the response interceptor consumes the (HttpResponse)InputStream which does not support mark, then nothing is left for the consumer eventually.
openapi-generator version
7.9.0
Steps to reproduce
api.mustache:
HttpResponse localVarResponse = memberVarHttpClient.send(
localVarRequestBuilder.build(),
HttpResponse.BodyHandlers.ofInputStream());
if (memberVarResponseInterceptor != null) {
memberVarResponseInterceptor.accept(localVarResponse);
}
In generated DefaultApi.java the InputStream is HttpResponseInputStream, which is non-marker InputStream,
so the Interceptor consumes it (and nothing lefts).
It would be great to be able to mark and reset the InputStream in the response interceptor.
Related issues/PRs
I could try to raise one to accelarating the fix.
Suggest a fix
Decorating the InputStream and delegate the response body.
For the shake of clarity I created a "BufferedHttpResponse" and tested it, it was working fine.
HttpResponse<InputStream> localVarResponse = new BufferedHttpResponse(memberVarHttpClient.send(
localVarRequestBuilder.build(),
HttpResponse.BodyHandlers.ofInputStream()));
if (memberVarResponseInterceptor != null) {
memberVarResponseInterceptor.accept(localVarResponse);
}
static class BufferedHttpResponse implements HttpResponse<InputStream> {
private HttpResponse<InputStream> delegate;
private BufferedInputStream bufferedInputStream;
public BufferedHttpResponse(HttpResponse<InputStream> delegate) {
this.delegate = delegate;
bufferedInputStream = new BufferedInputStream(delegate.body());
}
@Override
public int statusCode() { return delegate.statusCode(); } //etc...
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache at the HttpResponse and response-interceptor code shown in the issue. Trace how generated DefaultApi.java exposes the response body, then verify that an interceptor can read it without preventing the eventual consumer from reading it. Done means the generated native Java client supports reusable response-body reads while preserving the interceptor behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100