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

@@ -15,6 +15,7 @@
*/
package org.springframework.data.repository.core.support;
import static java.util.Arrays.*;
import static org.assertj.core.api.Assertions.*;
import io.vavr.control.Option;
@@ -27,6 +28,7 @@ import rx.Observable;
import rx.Single;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
@@ -36,6 +38,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.assertj.core.api.SoftAssertions;
import org.junit.Test;
import org.reactivestreams.Publisher;
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -47,6 +50,7 @@ import org.springframework.data.util.Streamable;
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Jens Schauder
*/
public class QueryExecutionResultHandlerUnitTests {
@@ -356,7 +360,7 @@ public class QueryExecutionResultHandlerUnitTests {
@SuppressWarnings("unchecked")
public void convertsIterableIntoStreamable() throws Exception {
Iterable<?> source = Arrays.asList(new Object());
Iterable<?> source = asList(new Object());
Object result = handler.postProcessInvocationResult(source, getMethod("streamable"));
@@ -384,6 +388,22 @@ public class QueryExecutionResultHandlerUnitTests {
});
}
@Test // DATACMNS-1482
public void nestedConversion() throws Exception {
Object result = handler.postProcessInvocationResult(asList(BigDecimal.ZERO, BigDecimal.ONE),
getMethod("listOfInteger"));
assertThat(result).isInstanceOf(List.class);
List list = (List) result;
SoftAssertions.assertSoftly(s -> {
// for making the test failure more obvious:
(list).forEach(v -> s.assertThat(v).isInstanceOf(Integer.class));
s.assertThat(list).containsExactly(0, 1);
});
}
private static Method getMethod(String methodName) throws Exception {
return Sample.class.getMethod(methodName);
}
@@ -421,6 +441,8 @@ public class QueryExecutionResultHandlerUnitTests {
// DATACMNS-1430
CustomStreamableWrapper<String> customStreamable();
List<Integer> listOfInteger();
}
static class Entity {}