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:
Oliver Gierke
2014-04-22 17:47:41 +02:00
parent c141fb771d
commit 248e314486
2 changed files with 32 additions and 8 deletions

View File

@@ -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;
}
}

View File

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