From 2839e9491fc3171e1869839bb7516c8548ed484e Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 4 Mar 2014 10:25:47 -0600 Subject: [PATCH] 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. --- .../repository/query/AbstractMongoQuery.java | 16 +++++++++------- ...AbstractPersonRepositoryIntegrationTests.java | 12 ++++++++++++ .../mongodb/repository/PersonRepository.java | 6 ++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java index 7feffa28c..7dab99565 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java @@ -82,24 +82,26 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { MongoParameterAccessor accessor = new MongoParametersParameterAccessor(method, parameters); Query query = createQuery(new ConvertingParameterAccessor(operations.getConverter(), accessor)); + Object result = null; + if (method.isGeoNearQuery() && method.isPageQuery()) { MongoParameterAccessor countAccessor = new MongoParametersParameterAccessor(method, parameters); Query countQuery = createCountQuery(new ConvertingParameterAccessor(operations.getConverter(), countAccessor)); - return new GeoNearExecution(accessor).execute(query, countQuery); + result = new GeoNearExecution(accessor).execute(query, countQuery); } else if (method.isGeoNearQuery()) { - return new GeoNearExecution(accessor).execute(query); + result = new GeoNearExecution(accessor).execute(query); } else if (method.isSliceQuery()) { - return new SlicedExecution(accessor.getPageable()).execute(query); + result = new SlicedExecution(accessor.getPageable()).execute(query); } else if (method.isCollectionQuery()) { - return new CollectionExecution(accessor.getPageable()).execute(query); + result = new CollectionExecution(accessor.getPageable()).execute(query); } else if (method.isPageQuery()) { - return new PagedExecution(accessor.getPageable()).execute(query); + result = new PagedExecution(accessor.getPageable()).execute(query); + } else { + result = new SingleEntityExecution(isCountQuery()).execute(query); } - Object result = new SingleEntityExecution(isCountQuery()).execute(query); - if (result == null) { return result; } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java index 32af2d790..e1820da66 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java @@ -750,4 +750,16 @@ public abstract class AbstractPersonRepositoryIntegrationTests { assertThat(result.hasNext(), is(true)); } + + /** + * @see DATAMONGO-871 + */ + @Test + public void findsPersonsByFirstnameAsArray() { + + Person[] result = repository.findByThePersonsFirstnameAsArray("Leroi"); + + assertThat(result, is(arrayWithSize(1))); + assertThat(result, is(arrayContaining(leroi))); + } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java index 2d639e32f..e4d1f0796 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java @@ -71,6 +71,12 @@ public interface PersonRepository extends MongoRepository, Query @Query(value = "{ 'firstname' : ?0 }", fields = "{ 'firstname': 1, 'lastname': 1}") List findByThePersonsFirstname(String firstname); + /** + * @see DATAMONGO-871 + */ + @Query(value = "{ 'firstname' : ?0 }") + Person[] findByThePersonsFirstnameAsArray(String firstname); + /** * Returns all {@link Person}s with a firstname matching the given one (*-wildcard supported). *