OpenAPITools / OpenAPITools/openapi-generator
[BUG][Java][Native] invalid generated code when binary/file responses are mapped to InputStream
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description & Observed Behavior
When generating a Java native client with supportStreaming=true and mapping binary / file types to java.io.InputStream, the generated response handling code contains invalid Java source code.
The generated API method signature correctly uses InputStream as the return type, but the internal implementation still follows the default path—downloading the payload into a temporary file and attempting to return a java.io.File.
This results in the following compilation error:
incompatible types: java.io.File cannot be converted to java.io.InputStream
Affected Configuration:
<supportStreaming>true</supportStreaming>
<typeMappings>
<typeMapping>file=InputStream</typeMapping>
<typeMapping>binary=InputStream</typeMapping>
</typeMappings>
<importMappings>
<importMapping>InputStream=java.io.InputStream</importMapping>
</importMappings>
Expected Behavior
If binary or file is explicitly mapped to InputStream, the generator should:
- Return the HTTP response body directly as an
InputStreamwithout downloading it to a temporaryFile. - Generate valid, compilable Java code.
- Preserve the default
Filehandling behavior if no custom mapping is specified.
Secondary Issue: Raw InputStream request body serialized as JSON
A related problem occurs when sending a raw application/octet-stream request body mapped to InputStream.
Instead of streaming the payload, the generated request builder attempts to serialize the InputStream object via Jackson:
byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body);
localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody));
This causes the client to send an empty JSON object ({}) instead of the actual binary stream.
Expected Fix for Request Body:
For InputStream / binary inputs, the generator should stream the body directly:
localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofInputStream(() -> body));
Standard application/json payloads should continue to use Jackson serialization.
Reproducer & Workaround
Attached is a minimal reproducer project containing:
- OpenAPI 3.0.3 specification
- Minimal Quarkus server (exposing
POST /binary/echoandPOST /binary/upload) - Generated Java
nativeclient with failing JUnit tests - A temporary, "quick-and-dirty"
api.mustachetemplate override demonstrating the issue area.
Note: Tested against the latest master branch. No existing open or closed issues matching this behavior were found.
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 the attached quick-and-dirty api.mustache override and the failing JUnit tests in the reproducer, then inspect the generated Java native client for response and request handling. Done means mapped InputStream responses and binary request bodies stream directly, generated code compiles, default File handling remains unchanged, and JSON payloads still use Jackson serialization.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100