From f7540d45c6857a40a0334c31bd29b36d7114d32f Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Tue, 24 Sep 2013 13:28:36 +0200 Subject: [PATCH] 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". --- .../core/aggregation/ProjectionOperation.java | 12 ++++++++++ .../ProjectionOperationUnitTests.java | 22 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java index ccb140ee5..81b122535 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java @@ -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 { List projections = new ArrayList(); 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)); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperationUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperationUnitTests.java index b91c5f685..65e092227 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperationUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperationUnitTests.java @@ -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 { assertThat(oper.get(MOD), is((Object) Arrays. 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() {