OpenAPITools / OpenAPITools/openapi-generator
[BUG] Description
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
A generated native API client does not handle a 204 response properly, if it also has a return type for 200 responses.
In case a 204 response is returned, the API tries to map the body to the return type but it fails.
The following snippet is of the api.mustache template:
if (localVarResponse.statusCode()/ 100 != 2) {
return CompletableFuture.failedFuture(getApiException("{{operationId}}", localVarResponse));
}
{{#returnType}}
try {
String responseBody = localVarResponse.body();
return CompletableFuture.completedFuture(
new ApiResponse<{{{returnType}}}>(
localVarResponse.statusCode(),
localVarResponse.headers().map(),
responseBody == null || responseBody.isBlank() ? null : memberVarObjectMapper.readValue(responseBody, new TypeReference<{{{returnType}}}>() {}))
);
} catch (IOException e) {
return CompletableFuture.failedFuture(new ApiException(e));
}
{{/returnType}}
For every 2xx response, the api tries to map the response to the return type which fails with a Jackson Exception cause of the empty body.
openapi-generator version
Version 7.9.0 but it seems to exist also in the latest version (7.12.0).
Steps to reproduce
Define an specification with a response with code 200 and a response body and a response with code 204. If a response with code 204 is returned during runtime, an ApiException with the Jackson exception as cause is thrown, it does not contain any status code or headers.
Suggest a fix
I see two ways to fix this issue:
- Do not attempt to map the body in case of a 204 and return an ApiResponse with null as body
- Throw also an ApiException but with correct status code and headers
I would go for the first solution as this is consistent with the behaviour in case ApiResponse is the return type and a 204 is returned.
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 api.mustache template and reproduce the issue using an OpenAPI specification containing 200 and 204 responses, then inspect the generated native Java client. Done means a 204 response no longer attempts to deserialize an empty body and returns an ApiResponse with the expected null body, status code, and headers.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100