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:
Oliver Gierke
2017-03-22 23:28:11 +01:00
parent fe5d543ff2
commit 429dcbf659
2 changed files with 18 additions and 10 deletions

View File

@@ -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)));
}
/**

View File

@@ -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]);