forked from loafle/openapi-generator-original
[Java][client] Fix feign classcastexception when getting headers (#16745)
* Avoid ClassCastException when getting headers (the header values are Collection<String> and not List<String>) Delete unused classes * Remove feign10x * Add unit test Refactor to avoid creating the headers map when ApiResponse is not used
This commit is contained in:
@@ -22,14 +22,13 @@ public class ApiResponseDecoder extends JacksonDecoder {
|
||||
|
||||
@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];
|
||||
Type responseBodyType = ((ParameterizedType) type).getActualTypeArguments()[0];
|
||||
Object body = super.decode(response, responseBodyType);
|
||||
return new ApiResponse(response.status(), responseHeaders, body);
|
||||
Map<String, Collection<String>> responseHeaders = Collections.unmodifiableMap(response.headers());
|
||||
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);
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package org.openapitools.client;
|
||||
|
||||
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 org.openapitools.client.model.HttpResponse;
|
||||
|
||||
public class JacksonResponseDecoder extends JacksonDecoder {
|
||||
|
||||
public JacksonResponseDecoder(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 HttpResponse
|
||||
Type responseBodyType;
|
||||
if (Types.getRawType(type).isAssignableFrom(HttpResponse.class)) {
|
||||
//The HttpResponse class has a single type parameter, the Dto class itself
|
||||
responseBodyType = ((ParameterizedType) type).getActualTypeArguments()[0];
|
||||
Object body = super.decode(response, responseBodyType);
|
||||
return new HttpResponse(responseHeaders, body, response.status());
|
||||
} else {
|
||||
//The response is not encapsulated in the HttpResponse, decode the Dto as normal
|
||||
return super.decode(response, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,19 @@
|
||||
package org.openapitools.client.model;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
import java.util.Collection;
|
||||
|
||||
public class ApiResponse<T>{
|
||||
|
||||
final private int statusCode;
|
||||
final private Map<String, List<String>> headers;
|
||||
final private Map<String, Collection<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) {
|
||||
public ApiResponse(int statusCode, Map<String, Collection<String>> headers) {
|
||||
this(statusCode, headers, null);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ public class ApiResponse<T>{
|
||||
* @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) {
|
||||
public ApiResponse(int statusCode, Map<String, Collection<String>> headers, T data) {
|
||||
this.statusCode = statusCode;
|
||||
this.headers = headers;
|
||||
this.data = data;
|
||||
@@ -32,7 +32,7 @@ public class ApiResponse<T>{
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
public Map<String, List<String>> getHeaders() {
|
||||
public Map<String, Collection<String>> getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
package org.openapitools.client.model;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Collection;
|
||||
|
||||
public class HttpResponse<T>{
|
||||
|
||||
private Map<String, Collection<String>> headers;
|
||||
|
||||
private T body;
|
||||
|
||||
private int status;
|
||||
|
||||
public HttpResponse(Map<String, Collection<String>> headers, T body, int status) {
|
||||
this.headers = headers;
|
||||
this.body = body;
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public T getBody(){
|
||||
return body;
|
||||
}
|
||||
|
||||
public Map<String, Collection<String>> getHeaders(){
|
||||
return headers;
|
||||
}
|
||||
|
||||
public int getStatus(){
|
||||
return status;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user