OpenAPITools / OpenAPITools/openapi-generator
[BUG] Assertion error when handling a 500 from server
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
I am using the openapi-generator-cli to generate java code. The generated code throws an assertion error while attempting to deserialize an empty response body coming from a 500 response.
openapi-generator version
7.8.0
OpenAPI declaration file content or url
Generated code will call getApiException when server returns a 500:
try {
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
localVarRequestBuilder.build(),
HttpResponse.BodyHandlers.ofInputStream());
if (memberVarResponseInterceptor != null) {
memberVarResponseInterceptor.accept(localVarResponse);
}
try {
if (localVarResponse.statusCode()/ 100 != 2) {
throw getApiException("something", localVarResponse);
}
The generated code for getApiException is the following:
protected ApiException getApiException(String operationId, HttpResponse<InputStream> response) throws IOException {
String body = response.body() == null ? null : new String(response.body().readAllBytes());
String message = formatExceptionMessage(operationId, response.statusCode(), body);
return new ApiException(response.statusCode(), message, response.headers(), body);
}
The response.body() is not null so the code will attempt to read the bytes of an empty response and trigger an assertion in the underlying int read(byte[] bytes, int off, int len) method:
public int read(byte[] bytes, int off, int len) throws IOException {
...
assert read > 0 && read <= buffer.remaining();
...
}
Generation Details
openapi-generator-cli generate --generator-key=client-java
Steps to reproduce
Create an endpoint that trhows a 500 with a empty body response.
Related issues/PRs
N/A
Suggest a fix
Do not attempt to deserialize the bytes if the response is null not null but empty.
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 client-java generation entry point from openapi-generator-cli generate --generator-key=client-java and inspect the generated getApiException path shown in the report. Reproduce a 500 response with an empty body, then add coverage for that case and verify that generation and the resulting request handling no longer trigger the assertion.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100