swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

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

Open ridvanab opened this issue 1 year ago • 0 comments

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.

ridvanab avatar Jul 18 '24 11:07 ridvanab