Fix ClassCastException if expected type is Object (#11510)

The currently generated code will throw a `ClassCastException` if `type` is `Object`. I'm not quite sure why `isAssignableFrom` is used here but I tried to keep the change minimal - in my understanding, the result would be the same if the line was 
```
if(Types.getRawType(type).equals(ApiResponse.class)) {
``` 
because the `ApiResponse` only extends `Object` and implements no interfaces. Maybe it was meant to be `ApiResponse.class.isAssignableFrom(Types.getRawType(type))`? This would also permit subclasses of `ApiResponse`, but then it would be more complicated to determine the actual type parameter (because `type` itself may not be parameterized) and to create the object to return (because it would have to be an instance of the subclass).
This commit is contained in:
lbilger 2022-02-05 07:43:25 +01:00 committed by GitHub
parent 92ccb629e9
commit 1a14d9e5ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,7 +25,7 @@ public class ApiResponseDecoder extends JacksonDecoder {
Map<String, Collection<String>> responseHeaders = Collections.unmodifiableMap(response.headers()); Map<String, Collection<String>> responseHeaders = Collections.unmodifiableMap(response.headers());
//Detects if the type is an instance of the parameterized class ApiResponse //Detects if the type is an instance of the parameterized class ApiResponse
Type responseBodyType; Type responseBodyType;
if (Types.getRawType(type).isAssignableFrom(ApiResponse.class)) { if (type instanceof ParameterizedType && Types.getRawType(type).isAssignableFrom(ApiResponse.class)) {
//The ApiResponse class has a single type parameter, the Dto class itself //The ApiResponse class has a single type parameter, the Dto class itself
responseBodyType = ((ParameterizedType) type).getActualTypeArguments()[0]; responseBodyType = ((ParameterizedType) type).getActualTypeArguments()[0];
Object body = super.decode(response, responseBodyType); Object body = super.decode(response, responseBodyType);
@ -35,4 +35,4 @@ public class ApiResponseDecoder extends JacksonDecoder {
return super.decode(response, type); return super.decode(response, type);
} }
} }
} }