DATACMNS-1482 - Properly convert collections when collection type matches but element type doesn't.

Various fast returns and the use of Class instead of TypeDescriptor led to e.g. List<BigDecimal> not getting properly converted to List<Integer> leading to unexpected ClassCastExceptions when the collection elements where accessed.
This commit is contained in:
Jens Schauder
2019-02-20 10:59:07 +01:00
committed by Oliver Drotbohm
parent f65fd511ac
commit 6dc0ced366
2 changed files with 26 additions and 19 deletions

View File

@@ -34,6 +34,7 @@ import org.springframework.lang.Nullable;
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Jens Schauder
*/
class QueryExecutionResultHandler {
@@ -63,10 +64,6 @@ class QueryExecutionResultHandler {
@Nullable
public Object postProcessInvocationResult(@Nullable Object result, Method method) {
if (method.getReturnType().isInstance(result)) {
return result;
}
MethodParameter parameter = new MethodParameter(method, -1);
return postProcessInvocationResult(result, 0, parameter);
@@ -91,20 +88,8 @@ class QueryExecutionResultHandler {
Class<?> expectedReturnType = returnTypeDescriptor.getType();
// Early return if the raw value matches
if (result != null && expectedReturnType.isInstance(result)) {
return result;
}
result = unwrapOptional(result);
// Early return if the unrwapped value matches
if (result != null && expectedReturnType.isInstance(result)) {
return result;
}
if (QueryExecutionConverters.supports(expectedReturnType)) {
// For a wrapper type, try nested resolution first
@@ -132,8 +117,8 @@ class QueryExecutionResultHandler {
return ReactiveWrapperConverters.toWrapper(result, expectedReturnType);
}
return conversionService.canConvert(result.getClass(), expectedReturnType)
? conversionService.convert(result, expectedReturnType)
return conversionService.canConvert(TypeDescriptor.forObject(result), returnTypeDescriptor)
? conversionService.convert(result, returnTypeDescriptor)
: result;
}