DATACMNS-483 - Added test case for recursive object wrapper resolution.
AbstractRepositoryMetadata now correctly resolves the domain type in case it's nested in multiple levels of wrapper types.
This commit is contained in:
@@ -57,14 +57,7 @@ public abstract class AbstractRepositoryMetadata implements RepositoryMetadata {
|
||||
* @see org.springframework.data.repository.core.RepositoryMetadata#getReturnedDomainClass(java.lang.reflect.Method)
|
||||
*/
|
||||
public Class<?> getReturnedDomainClass(Method method) {
|
||||
|
||||
TypeInformation<?> returnTypeInfo = typeInformation.getReturnType(method);
|
||||
Class<?> rawType = returnTypeInfo.getType();
|
||||
|
||||
boolean needToUnwrap = Iterable.class.isAssignableFrom(rawType) || rawType.isArray()
|
||||
|| QueryExecutionConverters.supports(rawType);
|
||||
|
||||
return needToUnwrap ? returnTypeInfo.getComponentType().getType() : rawType;
|
||||
return unwrapWrapperTypes(typeInformation.getReturnType(method));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -104,4 +97,20 @@ public abstract class AbstractRepositoryMetadata implements RepositoryMetadata {
|
||||
|
||||
return Arrays.asList(findAllMethod.getParameterTypes()).contains(Pageable.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively unwraps well known wrapper types from the given {@link TypeInformation}.
|
||||
*
|
||||
* @param type must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
private static Class<?> unwrapWrapperTypes(TypeInformation<?> type) {
|
||||
|
||||
Class<?> rawType = type.getType();
|
||||
|
||||
boolean needToUnwrap = Iterable.class.isAssignableFrom(rawType) || rawType.isArray()
|
||||
|| QueryExecutionConverters.supports(rawType);
|
||||
|
||||
return needToUnwrap ? unwrapWrapperTypes(type.getComponentType()) : rawType;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,6 +117,18 @@ public class DefaultRepositoryMetadataUnitTests {
|
||||
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(User.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-483
|
||||
*/
|
||||
@Test
|
||||
public void discoversDomainTypeOnNestedReturnTypeWrapper() throws Exception {
|
||||
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(OptionalRepository.class);
|
||||
|
||||
Method method = OptionalRepository.class.getMethod("findByLastname", String.class);
|
||||
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(User.class)));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private class User {
|
||||
|
||||
@@ -187,5 +199,8 @@ public class DefaultRepositoryMetadataUnitTests {
|
||||
static interface OptionalRepository extends Repository<User, Long> {
|
||||
|
||||
Optional<User> findByEmailAddress(String emailAddress);
|
||||
|
||||
// Contrived example but to make sure recursive wrapper resolution works
|
||||
Optional<Optional<User>> findByLastname(String lastname);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user