Skip to content

Commit ae03fea

Browse files
committed
DATACMNS-460 - Repository metadata now supports array return types for query methods.
Added array checks to AbstractRepositoryMetadata.getReturnedDomainClass() to detect array component types. QueryMethod now considers methods returning arrays to be collection query methods.
1 parent 1805aad commit ae03fea

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public Class<?> getReturnedDomainClass(Method method) {
6060
TypeInformation<?> returnTypeInfo = typeInformation.getReturnType(method);
6161
Class<?> rawType = returnTypeInfo.getType();
6262

63-
return Iterable.class.isAssignableFrom(rawType) ? returnTypeInfo.getComponentType().getType() : rawType;
63+
boolean needToUnwrap = Iterable.class.isAssignableFrom(rawType) || rawType.isArray();
64+
65+
return needToUnwrap ? returnTypeInfo.getComponentType().getType() : rawType;
6466
}
6567

6668
/*

src/main/java/org/springframework/data/repository/query/QueryMethod.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2013 the original author or authors.
2+
* Copyright 2008-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -154,7 +154,7 @@ public boolean isCollectionQuery() {
154154

155155
Class<?> returnType = method.getReturnType();
156156
return !(isPageQuery() || isSliceQuery())
157-
&& org.springframework.util.ClassUtils.isAssignable(Iterable.class, returnType);
157+
&& org.springframework.util.ClassUtils.isAssignable(Iterable.class, returnType) || returnType.isArray();
158158
}
159159

160160
/**

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
*/
4141
public class AbstractRepositoryMetadataUnitTests {
4242

43+
/**
44+
* @see DATACMNS-98
45+
*/
4346
@Test
4447
public void discoversSimpleReturnTypeCorrectly() throws Exception {
4548

@@ -58,6 +61,9 @@ public void resolvesTypeParameterReturnType() throws Exception {
5861
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(User.class)));
5962
}
6063

64+
/**
65+
* @see DATACMNS-98
66+
*/
6167
@Test
6268
public void determinesReturnTypeFromPageable() throws Exception {
6369

@@ -86,13 +92,19 @@ public void pageableRepository() {
8692
assertThat(metadata.isPagingRepository(), is(true));
8793
}
8894

95+
/**
96+
* @see DATACMNS-98
97+
*/
8998
@Test
9099
public void determinesReturnTypeFromGenericType() throws Exception {
91100
RepositoryMetadata metadata = new DummyRepositoryMetadata(ExtendingRepository.class);
92101
Method method = ExtendingRepository.class.getMethod("someMethod");
93102
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(GenericType.class)));
94103
}
95104

105+
/**
106+
* @see DATACMNS-98
107+
*/
96108
@Test
97109
public void handlesGenericTypeInReturnedCollectionCorrectly() throws SecurityException, NoSuchMethodException {
98110

@@ -101,6 +113,18 @@ public void handlesGenericTypeInReturnedCollectionCorrectly() throws SecurityExc
101113
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(Map.class)));
102114
}
103115

116+
/**
117+
* @see DATACMNS-471
118+
*/
119+
@Test
120+
public void detectsArrayReturnTypeCorrectly() throws Exception {
121+
122+
RepositoryMetadata metadata = new DefaultRepositoryMetadata(PagedRepository.class);
123+
Method method = PagedRepository.class.getMethod("returnsArray");
124+
125+
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(User.class)));
126+
}
127+
104128
interface UserRepository extends Repository<User, Long> {
105129

106130
User findSingle();
@@ -126,6 +150,7 @@ interface ExtendingRepository extends Serializable, UserRepository {
126150

127151
interface PagedRepository extends PagingAndSortingRepository<User, Long> {
128152

153+
User[] returnsArray();
129154
}
130155

131156
class GenericType<T> {

src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ public void detectsSliceMethod() throws Exception {
126126
assertThat(queryMethod.isPageQuery(), is(false));
127127
}
128128

129+
/**
130+
* @see DATACMNS-471
131+
*/
132+
@Test
133+
public void detectsCollectionMethodForArrayRetrunType() throws Exception {
134+
135+
RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(SampleRepository.class);
136+
Method method = SampleRepository.class.getMethod("arrayOfUsers");
137+
138+
assertThat(new QueryMethod(method, repositoryMetadata).isCollectionQuery(), is(true));
139+
}
140+
129141
interface SampleRepository extends Repository<User, Serializable> {
130142

131143
String pagingMethodWithInvalidReturnType(Pageable pageable);
@@ -143,6 +155,8 @@ interface SampleRepository extends Repository<User, Serializable> {
143155
Integer returnsProjection();
144156

145157
Slice<User> sliceOfUsers();
158+
159+
User[] arrayOfUsers();
146160
}
147161

148162
class User {

0 commit comments

Comments
 (0)