Skip to content

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

Open
@sbrossie

Description

@sbrossie

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions