DATAMONGO-1752 - Fixed execution of repository query methods with closed projections.
We now skip the explicit definition of which type to read in case a closed projection is defined as return type.
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -286,7 +286,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
Page<Person> findTop3ByLastnameStartingWith(String lastname, Pageable pageRequest);
|
||||
|
||||
// DATAMONGO-1030
|
||||
PersonSummary findSummaryByLastname(String lastname);
|
||||
PersonSummaryDto findSummaryByLastname(String lastname);
|
||||
|
||||
@Query("{ ?0 : ?1 }")
|
||||
List<Person> findByKeyValue(String key, String value);
|
||||
@@ -325,4 +325,10 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
*/
|
||||
@DeleteQuery("{ 'firstname' : ?0 }") // DATAMONGO-1539
|
||||
void deleteByThePersonsFirstname(String firstname);
|
||||
|
||||
// DATAMONGO-1752
|
||||
Iterable<PersonExcerpt> findOpenProjectionBy();
|
||||
|
||||
// DATAMONGO-1752
|
||||
Iterable<PersonSummary> findClosedProjectionBy();
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user