DATACMNS-867 - ReflectionRepositoryInvoker now avoids ConversionService for Optional creation.
We now manually create Optional instances as the ConversionService converts single-argument collections into an optional with only that element.
This commit is contained in:
@@ -253,16 +253,8 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> Optional<T> returnAsOptional(Object source) {
|
||||
|
||||
if (Optional.class.isInstance(source)) {
|
||||
return (Optional<T>) source;
|
||||
}
|
||||
|
||||
if (source == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return conversionService.convert(QueryExecutionConverters.unwrap(source), Optional.class);
|
||||
return (Optional<T>) (Optional.class.isInstance(source) ? source
|
||||
: Optional.ofNullable(QueryExecutionConverters.unwrap(source)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -254,6 +254,22 @@ public class ReflectionRepositoryInvokerUnitTests {
|
||||
assertThat(invokeFindOne).isPresent();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-867
|
||||
public void wrapsSingleElementCollectionIntoOptional() throws Exception {
|
||||
|
||||
ManualCrudRepository mock = mock(ManualCrudRepository.class);
|
||||
when(mock.findAll()).thenReturn(Arrays.asList(new Domain()));
|
||||
|
||||
Method method = ManualCrudRepository.class.getMethod("findAll");
|
||||
|
||||
Optional<Object> result = getInvokerFor(mock).invokeQueryMethod(method, new LinkedMultiValueMap<>(), Pageable.unpaged(),
|
||||
Sort.unsorted());
|
||||
|
||||
assertThat(result).hasValueSatisfying(it -> {
|
||||
assertThat(it).isInstanceOf(Collection.class);
|
||||
});
|
||||
}
|
||||
|
||||
private static RepositoryInvoker getInvokerFor(Object repository) {
|
||||
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(repository.getClass().getInterfaces()[0]);
|
||||
|
||||
Reference in New Issue
Block a user