Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Assertion error when handling a 500 from server #20081

Open
5 tasks done
sbrossie opened this issue Nov 11, 2024 · 0 comments
Open
5 tasks done

[BUG] Assertion error when handling a 500 from server #20081

sbrossie opened this issue Nov 11, 2024 · 0 comments

Comments

@sbrossie
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant