swagger-api / swagger-api/swagger-codegen

[JAVA][Jersey2][Regression] Inconsistency between ApiClient deserialisation method for swagger2 and swagger3

Open
#12,424 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

During update, we have encountered an issue regarding the generation of the ApiClient.class.

Seems like swagger3 does not have the following issue fixed over from swagger2:
https://github.com/swagger-api/swagger-codegen/issues/6226
and the following PR that fixes it:
https://github.com/swagger-api/swagger-codegen/pull/6977

Due to this, there is the issue of ApiClient failing to deserialise requests with responses without content-type headers.

Swagger3 method:

  /**
   * Deserialize response body to Java object according to the Content-Type.
   * @param <T> Type
   * @param response Response
   * @param returnType Return type
   * @return Deserialize object
   * @throws ApiException API exception
   */
  @SuppressWarnings("unchecked")
  public <T> T deserialize(Response response, GenericType<T> returnType) throws ApiException {
    if (response == null || returnType == null) {
      return null;
    }

    if ("byte[]".equals(returnType.toString())) {
      // Handle binary response (byte array).
      return (T) response.readEntity(byte[].class);
    } else if (returnType.getRawType() == File.class) {
      // Handle file downloading.
      T file = (T) downloadFileFromResponse(response);
      return file;
    }

    String contentType = null;
    List<Object> contentTypes = response.getHeaders().get("Content-Type");
    if (contentTypes != null && !contentTypes.isEmpty())
      contentType = String.valueOf(contentTypes.get(0));
    if (contentType == null)
      throw new ApiException(500, "missing Content-Type in response");

    return response.readEntity(returnType);
  }

Swagger2 method:

/**
   * Deserialize response body to Java object according to the Content-Type.
   * @param <T> Type
   * @param response Response
   * @param returnType Return type
   * @return Deserialize object
   * @throws ApiException API exception
   */
  @SuppressWarnings("unchecked")
  public <T> T deserialize(Response response, GenericType<T> returnType) throws ApiException {
    if (response == null || returnType == null) {
      return null;
    }

    if ("byte[]".equals(returnType.toString())) {
      // Handle binary response (byte array).
      return (T) response.readEntity(byte[].class);
    } else if (returnType.getRawType() == File.class) {
      // Handle file downloading.
      T file = (T) downloadFileFromResponse(response);
      return file;
    }

    String contentType = null;
    List<Object> contentTypes = response.getHeaders().get("Content-Type");
    if (contentTypes != null && !contentTypes.isEmpty())
      contentType = String.valueOf(contentTypes.get(0));

    return response.readEntity(returnType);
  }
Swagger-codegen version

swagger3

Swagger declaration file content or url
Command line used for generation
Steps to reproduce

Generate a sample code both on swagger2 and swagger3. The ApiClient.class will differ.

Related issues/PRs

https://github.com/swagger-api/swagger-codegen/issues/6226
https://github.com/swagger-api/swagger-codegen/pull/6977

Suggest a fix/enhancement

This should be applied to swagger3 generation as well.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how swagger3 generation produces the Java ApiClient.class and inspect its deserialize(Response, GenericType) method alongside the swagger2 version shown in the issue. Generate sample code for both versions and verify that a response without a Content-Type header can be deserialized without the swagger3-specific failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.