Skip to content

Commit 2839e94

Browse files
committed
DATAMONGO-871 - Add support for arrays as query method return types.
Changed AbstractMongoQuery to potentially convert all query execution results using the DefaultConversionService in case the query result doesn't match the expected return value. This allows arrays to be returned for collection queries as the conversion service cam transparently convert between collections and arrays.
1 parent 6963f9e commit 2839e94

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,26 @@ public Object execute(Object[] parameters) {
8282
MongoParameterAccessor accessor = new MongoParametersParameterAccessor(method, parameters);
8383
Query query = createQuery(new ConvertingParameterAccessor(operations.getConverter(), accessor));
8484

85+
Object result = null;
86+
8587
if (method.isGeoNearQuery() && method.isPageQuery()) {
8688

8789
MongoParameterAccessor countAccessor = new MongoParametersParameterAccessor(method, parameters);
8890
Query countQuery = createCountQuery(new ConvertingParameterAccessor(operations.getConverter(), countAccessor));
8991

90-
return new GeoNearExecution(accessor).execute(query, countQuery);
92+
result = new GeoNearExecution(accessor).execute(query, countQuery);
9193
} else if (method.isGeoNearQuery()) {
92-
return new GeoNearExecution(accessor).execute(query);
94+
result = new GeoNearExecution(accessor).execute(query);
9395
} else if (method.isSliceQuery()) {
94-
return new SlicedExecution(accessor.getPageable()).execute(query);
96+
result = new SlicedExecution(accessor.getPageable()).execute(query);
9597
} else if (method.isCollectionQuery()) {
96-
return new CollectionExecution(accessor.getPageable()).execute(query);
98+
result = new CollectionExecution(accessor.getPageable()).execute(query);
9799
} else if (method.isPageQuery()) {
98-
return new PagedExecution(accessor.getPageable()).execute(query);
100+
result = new PagedExecution(accessor.getPageable()).execute(query);
101+
} else {
102+
result = new SingleEntityExecution(isCountQuery()).execute(query);
99103
}
100104

101-
Object result = new SingleEntityExecution(isCountQuery()).execute(query);
102-
103105
if (result == null) {
104106
return result;
105107
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -750,4 +750,16 @@ public void findsSliceOfPersons() {
750750

751751
assertThat(result.hasNext(), is(true));
752752
}
753+
754+
/**
755+
* @see DATAMONGO-871
756+
*/
757+
@Test
758+
public void findsPersonsByFirstnameAsArray() {
759+
760+
Person[] result = repository.findByThePersonsFirstnameAsArray("Leroi");
761+
762+
assertThat(result, is(arrayWithSize(1)));
763+
assertThat(result, is(arrayContaining(leroi)));
764+
}
753765
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
7171
@Query(value = "{ 'firstname' : ?0 }", fields = "{ 'firstname': 1, 'lastname': 1}")
7272
List<Person> findByThePersonsFirstname(String firstname);
7373

74+
/**
75+
* @see DATAMONGO-871
76+
*/
77+
@Query(value = "{ 'firstname' : ?0 }")
78+
Person[] findByThePersonsFirstnameAsArray(String firstname);
79+
7480
/**
7581
* Returns all {@link Person}s with a firstname matching the given one (*-wildcard supported).
7682
*

0 commit comments

Comments
 (0)