OpenAPITools / OpenAPITools/openapi-generator

Java OpenFeign ApiResponse headers ClassCastException

Open
#15,550 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

When attempting to retrieve a header from the generated ApiResponse class, I get the following exception:

java.lang.ClassCastException: class java.util.Collections$UnmodifiableCollection cannot be cast to class java.util.List (java.util.Collections$UnmodifiableCollection and java.util.List are in module java.base of loader 'bootstrap')

Looking at modules/openapi-generator/src/main/resources/Java/libraries/feign/model/ApiResponse.mustache:

package {{modelPackage}};

import java.util.Map;
import java.util.List;

public class ApiResponse<T>{

    final private int statusCode;
    final private Map<String, List<String>> headers;
    final private T data;

    /**
     * @param statusCode The status code of HTTP response
     * @param headers The headers of HTTP response
     */
    public ApiResponse(int statusCode, Map<String, List<String>> headers) {
        this(statusCode, headers, null);
    }

    /**
     * @param statusCode The status code of HTTP response
     * @param headers The headers of HTTP response
     * @param data The object deserialized from response bod
     */
    public ApiResponse(int statusCode, Map<String, List<String>> headers, T data) {
        this.statusCode = statusCode;
        this.headers = headers;
        this.data = data;
    }

    public int getStatusCode() {
        return statusCode;
    }

    public Map<String, List<String>> getHeaders() {
        return headers;
    }

    public T getData() {
        return data;
    }
  
}

Here we see that headers are of type Map<String, List>

However, when we look at modules/openapi-generator/src/main/resources/Java/libraries/feign/ApiResponseDecoder.mustache:

package {{invokerPackage}};

import com.fasterxml.jackson.databind.ObjectMapper;
import feign.Response;
import feign.Types;
import feign.jackson.JacksonDecoder;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import {{modelPackage}}.ApiResponse;

public class ApiResponseDecoder extends JacksonDecoder {

    public ApiResponseDecoder(ObjectMapper mapper) {
        super(mapper);
    }

    @Override
    public Object decode(Response response, Type type) throws IOException {
    Map<String, Collection<String>> responseHeaders = Collections.unmodifiableMap(response.headers());
        //Detects if the type is an instance of the parameterized class ApiResponse
        Type responseBodyType;
        if (type instanceof ParameterizedType && Types.getRawType(type).isAssignableFrom(ApiResponse.class)) {
            //The ApiResponse class has a single type parameter, the Dto class itself
            responseBodyType = ((ParameterizedType) type).getActualTypeArguments()[0];
            Object body = super.decode(response, responseBodyType);
            return new ApiResponse(response.status(), responseHeaders, body);
        } else {
            //The response is not encapsulated in the ApiResponse, decode the Dto as normal
            return super.decode(response, type);
        }
    }
}

We see that the responseHeaders is of type Map<String, Collection<String>>

openapi-generator version

6.6.0

Suggest a fix/enhancement

We should either change ApiResponse.mustache to have headers be of type Map<String, Collection<String>> or change ApiResponseDecoder.mustache to ensure it's passing in a collection of type Map<String, List<String>>

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 comparing modules/openapi-generator/src/main/resources/Java/libraries/feign/model/ApiResponse.mustache with modules/openapi-generator/src/main/resources/Java/libraries/feign/ApiResponseDecoder.mustache, focusing on the declared header value types. Verify the generated Feign ApiResponse path with a response containing headers; done means header retrieval no longer causes the reported ClassCastException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.