Skip to content

Commit 248e314

Browse files
committed
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.
1 parent c141fb7 commit 248e314

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/main/java/org/springframework/data/repository/core/support/AbstractRepositoryMetadata.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,7 @@ public AbstractRepositoryMetadata(Class<?> repositoryInterface) {
5757
* @see org.springframework.data.repository.core.RepositoryMetadata#getReturnedDomainClass(java.lang.reflect.Method)
5858
*/
5959
public Class<?> getReturnedDomainClass(Method method) {
60-
61-
TypeInformation<?> returnTypeInfo = typeInformation.getReturnType(method);
62-
Class<?> rawType = returnTypeInfo.getType();
63-
64-
boolean needToUnwrap = Iterable.class.isAssignableFrom(rawType) || rawType.isArray()
65-
|| QueryExecutionConverters.supports(rawType);
66-
67-
return needToUnwrap ? returnTypeInfo.getComponentType().getType() : rawType;
60+
return unwrapWrapperTypes(typeInformation.getReturnType(method));
6861
}
6962

7063
/*
@@ -104,4 +97,20 @@ public boolean isPagingRepository() {
10497

10598
return Arrays.asList(findAllMethod.getParameterTypes()).contains(Pageable.class);
10699
}
100+
101+
/**
102+
* Recursively unwraps well known wrapper types from the given {@link TypeInformation}.
103+
*
104+
* @param type must not be {@literal null}.
105+
* @return
106+
*/
107+
private static Class<?> unwrapWrapperTypes(TypeInformation<?> type) {
108+
109+
Class<?> rawType = type.getType();
110+
111+
boolean needToUnwrap = Iterable.class.isAssignableFrom(rawType) || rawType.isArray()
112+
|| QueryExecutionConverters.supports(rawType);
113+
114+
return needToUnwrap ? unwrapWrapperTypes(type.getComponentType()) : rawType;
115+
}
107116
}

src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryMetadataUnitTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ public void discoversDomainTypeOnReturnTypeWrapper() throws Exception {
117117
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(User.class)));
118118
}
119119

120+
/**
121+
* @see DATACMNS-483
122+
*/
123+
@Test
124+
public void discoversDomainTypeOnNestedReturnTypeWrapper() throws Exception {
125+
126+
RepositoryMetadata metadata = new DefaultRepositoryMetadata(OptionalRepository.class);
127+
128+
Method method = OptionalRepository.class.getMethod("findByLastname", String.class);
129+
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(User.class)));
130+
}
131+
120132
@SuppressWarnings("unused")
121133
private class User {
122134

@@ -187,5 +199,8 @@ static interface ConcreteRepository extends IdTypeFixingRepository<User> {
187199
static interface OptionalRepository extends Repository<User, Long> {
188200

189201
Optional<User> findByEmailAddress(String emailAddress);
202+
203+
// Contrived example but to make sure recursive wrapper resolution works
204+
Optional<Optional<User>> findByLastname(String lastname);
190205
}
191206
}

0 commit comments

Comments
 (0)