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 35147fadd..cc96ac4ae 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 @@ -86,8 +86,11 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { applyQueryMetaAttributesWhenPresent(query); ResultProcessor processor = method.getResultProcessor().withDynamicProjection(accessor); - ReturnedType returnedType = processor.getReturnedType(); - FindWithQuery find = findOperationWithProjection.as(returnedType.getTypeToRead()); + Class typeToRead = processor.getReturnedType().getTypeToRead(); + + FindWithQuery find = typeToRead == null // + ? findOperationWithProjection // + : findOperationWithProjection.as(typeToRead); MongoQueryExecution execution = getExecution(accessor, find); 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 fd5f9bf1f..3a5e1abd5 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 @@ -16,8 +16,10 @@ package org.springframework.data.mongodb.repository; import static java.util.Arrays.*; +import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertThat; import static org.springframework.data.geo.Metrics.*; import java.util.ArrayList; @@ -928,7 +930,7 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @Test // DATAMONGO-1030 public void executesSingleEntityQueryWithProjectionCorrectly() { - PersonSummary result = repository.findSummaryByLastname("Beauford"); + PersonSummaryDto result = repository.findSummaryByLastname("Beauford"); assertThat(result, is(notNullValue())); assertThat(result.firstname, is("Carter")); @@ -1165,4 +1167,14 @@ public abstract class AbstractPersonRepositoryIntegrationTests { assertThat(repository.countByThePersonsFirstname("Dave"), is(0L)); } + + @Test // DATAMONGO-1752 + public void readsOpenProjection() { + assertThat(repository.findOpenProjectionBy()).isNotEmpty(); + } + + @Test // DATAMONGO-1752 + public void readsClosedProjection() { + assertThat(repository.findClosedProjectionBy()).isNotEmpty(); + } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonExcerpt.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonExcerpt.java new file mode 100644 index 000000000..cdfff8f6b --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonExcerpt.java @@ -0,0 +1,27 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.repository; + +import org.springframework.beans.factory.annotation.Value; + +/** + * @author Oliver Gierke + */ +public interface PersonExcerpt { + + @Value("#{target.firstname + ' ' + target.lastname}") + String getFullName(); +} 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 120aac56d..64791add4 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 @@ -286,7 +286,7 @@ public interface PersonRepository extends MongoRepository, Query Page findTop3ByLastnameStartingWith(String lastname, Pageable pageRequest); // DATAMONGO-1030 - PersonSummary findSummaryByLastname(String lastname); + PersonSummaryDto findSummaryByLastname(String lastname); @Query("{ ?0 : ?1 }") List findByKeyValue(String key, String value); @@ -325,4 +325,10 @@ public interface PersonRepository extends MongoRepository, Query */ @DeleteQuery("{ 'firstname' : ?0 }") // DATAMONGO-1539 void deleteByThePersonsFirstname(String firstname); + + // DATAMONGO-1752 + Iterable findOpenProjectionBy(); + + // DATAMONGO-1752 + Iterable findClosedProjectionBy(); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonSummary.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonSummary.java index d7caf6069..7f0b0b334 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonSummary.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonSummary.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,8 +18,9 @@ package org.springframework.data.mongodb.repository; /** * @author Oliver Gierke */ -public class PersonSummary { +public interface PersonSummary { - String firstname; - String lastname; + String getFirstname(); + + String getLastname(); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonSummaryDto.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonSummaryDto.java new file mode 100644 index 000000000..3cf3505f2 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonSummaryDto.java @@ -0,0 +1,25 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.repository; + +/** + * @author Oliver Gierke + */ +public class PersonSummaryDto { + + String firstname; + String lastname; +}