Browse Source

DATAMONGO-758 - Current mongodb Versions only support to explicitly exclude the _id property in a projection.

Added guard to FieldProjection.from within ProjectionOption to prevent users from excluding fields other than "_id".
pull/88/head
Thomas Darimont 12 years ago committed by Oliver Gierke
parent
commit
f7540d45c6
  1. 12
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java
  2. 22
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperationUnitTests.java

12
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java

@ -362,6 +362,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation { @@ -362,6 +362,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
* A {@link FieldProjection} to map a result of a previous {@link AggregationOperation} to a new field.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
static class FieldProjection extends Projection {
@ -399,6 +400,17 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation { @@ -399,6 +400,17 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
List<FieldProjection> projections = new ArrayList<FieldProjection>();
for (Field field : fields) {
if (!include) {
if (!Fields.UNDERSCORE_ID.equals(field.getName())) {
throw new IllegalArgumentException(
String
.format(
"Exclusion of field %s not allowed. Projections by the mongodb aggregation framework only support the exclusion of the %s field!",
field.getName(), Fields.UNDERSCORE_ID));
}
}
projections.add(new FieldProjection(field, include ? null : 0));
}

22
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperationUnitTests.java

@ -31,6 +31,7 @@ import com.mongodb.DBObject; @@ -31,6 +31,7 @@ import com.mongodb.DBObject;
* Unit tests for {@link ProjectionOperation}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class ProjectionOperationUnitTests {
@ -183,6 +184,27 @@ public class ProjectionOperationUnitTests { @@ -183,6 +184,27 @@ public class ProjectionOperationUnitTests {
assertThat(oper.get(MOD), is((Object) Arrays.<Object> asList("$a", 3)));
}
/**
* @see DATAMONGO-758
*/
@Test(expected = IllegalArgumentException.class)
public void excludeShouldThrowExceptionForFieldsOtherThanUnderscoreId() {
new ProjectionOperation().andExclude("foo");
}
/**
* @see DATAMONGO-758
*/
@Test
public void excludeShouldExclusionOfUnderscoreId() {
ProjectionOperation projectionOp = new ProjectionOperation().andExclude(Fields.UNDERSCORE_ID);
DBObject dbObject = projectionOp.toDBObject(Aggregation.DEFAULT_CONTEXT);
DBObject projectClause = DBObjectUtils.getAsDBObject(dbObject, PROJECT);
assertThat((Integer) projectClause.get(Fields.UNDERSCORE_ID), is(0));
}
@Test(expected = IllegalArgumentException.class)
public void arithmenticProjectionOperationModByZeroException() {

Loading…
Cancel
Save