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".
This commit is contained in:
committed by
Oliver Gierke
parent
3d2ae8117f
commit
f7540d45c6
@@ -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<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));
|
||||
}
|
||||
|
||||
|
||||
@@ -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.<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() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user