Browse Source

DATAMONGO-1729 - Open projections don't get field restrictions applied.

We now only apply a field restriction if the projection used for a query is closed.
pull/482/head
Oliver Gierke 9 years ago
parent
commit
d365463f56
  1. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQuery.java
  2. 19
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQueryUnitTests.java

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

@ -98,7 +98,7 @@ public class PartTreeMongoQuery extends AbstractMongoQuery { @@ -98,7 +98,7 @@ public class PartTreeMongoQuery extends AbstractMongoQuery {
ReturnedType returnedType = processor.withDynamicProjection(accessor).getReturnedType();
if (returnedType.isProjecting()) {
if (returnedType.needsCustomConstruction()) {
Field fields = query.fields();

19
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQueryUnitTests.java

@ -30,6 +30,7 @@ import org.junit.rules.ExpectedException; @@ -30,6 +30,7 @@ import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
@ -206,6 +207,14 @@ public class PartTreeMongoQueryUnitTests { @@ -206,6 +207,14 @@ public class PartTreeMongoQueryUnitTests {
assertThat(query.getFieldsObject().get("firstname"), is((Object) 1));
}
@Test // DATAMONGO-1729
public void doesNotCreateFieldsObjectForOpenProjection() {
org.springframework.data.mongodb.core.query.Query query = deriveQueryFromMethod("findAllBy");
assertThat(query.getFieldsObject(), is(nullValue()));
}
private org.springframework.data.mongodb.core.query.Query deriveQueryFromMethod(String method, Object... args) {
Class<?>[] types = new Class<?>[args.length];
@ -266,6 +275,8 @@ public class PartTreeMongoQueryUnitTests { @@ -266,6 +275,8 @@ public class PartTreeMongoQueryUnitTests {
@Query(fields = "{ 'firstname' : 1 }")
List<Person> findBySex(Sex sex);
OpenProjection findAllBy();
}
interface PersonProjection {
@ -290,4 +301,12 @@ public class PartTreeMongoQueryUnitTests { @@ -290,4 +301,12 @@ public class PartTreeMongoQueryUnitTests {
this.lastname = lastname;
}
}
interface OpenProjection {
String getFirstname();
@Value("#{target.firstname + ' ' + target.lastname}")
String getFullname();
}
}

Loading…
Cancel
Save