DATAMONGO-1756 - Fix nested field name resolution for arithmetic aggregation ops.

Original pull request: #491.
This commit is contained in:
Christoph Strobl
2017-07-28 08:53:25 +02:00
committed by Mark Paluch
parent 9c43ece3a7
commit 71135395c1
3 changed files with 38 additions and 3 deletions

View File

@@ -1408,7 +1408,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
protected List<Object> getOperationArguments(AggregationOperationContext context) {
List<Object> result = new ArrayList<Object>(values.size());
result.add(context.getReference(getField().getName()).toString());
result.add(context.getReference(getField()).toString());
for (Object element : values) {

View File

@@ -40,7 +40,7 @@ import com.mongodb.util.JSON;
/**
* Unit tests for {@link Aggregation}.
*
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
@@ -564,6 +564,16 @@ public class AggregationUnitTests {
assertThat(getAsDBObject(fields, "foosum"), isBsonObject().containing("$first.$cond.else", "no-answer"));
}
@Test // DATAMONGO-1756
public void projectOperationShouldRenderNestedFieldNamesCorrectly() {
DBObject agg = newAggregation(project().and("value1.value").plus("value2.value").as("val")).toDbObject("collection",
Aggregation.DEFAULT_CONTEXT);
assertThat((BasicDBObject) extractPipelineElement(agg, 0, "$project"), is(equalTo(new BasicDBObject("val",
new BasicDBObject("$add", new BasicDbListBuilder().add("$value1.value").add("$value2.value").get())))));
}
private DBObject extractPipelineElement(DBObject agg, int index, String operation) {
List<DBObject> pipeline = (List<DBObject>) agg.get("pipeline");

View File

@@ -47,6 +47,7 @@ import org.springframework.data.mongodb.core.convert.QueryMapper;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.test.util.BasicDbListBuilder;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
@@ -54,7 +55,7 @@ import com.mongodb.util.JSON;
/**
* Unit tests for {@link TypeBasedAggregationOperationContext}.
*
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Mark Paluch
@@ -336,6 +337,19 @@ public class TypeBasedAggregationOperationContextUnitTests {
assertThat(age, isBsonObject().containing("$ifNull.[1]._class", Age.class.getName()));
}
@Test // DATAMONGO-1756
public void projectOperationShouldRenderNestedFieldNamesCorrectlyForTypedAggregation() {
AggregationOperationContext context = getContext(Wrapper.class);
DBObject agg = newAggregation(Wrapper.class, project().and("nested1.value1").plus("nested2.value2").as("val"))
.toDbObject("collection", context);
BasicDBObject project = (BasicDBObject) getPipelineElementFromAggregationAt(agg, 0).get("$project");
assertThat(project, is(equalTo(new BasicDBObject("val", new BasicDBObject("$add",
new BasicDbListBuilder().add("$nested1.value1").add("$field2.nestedValue2").get())))));
}
@Document(collection = "person")
public static class FooPerson {
@@ -406,4 +420,15 @@ public class TypeBasedAggregationOperationContextUnitTests {
String name;
}
static class Wrapper {
Nested nested1;
@org.springframework.data.mongodb.core.mapping.Field("field2") Nested nested2;
}
static class Nested {
String value1;
@org.springframework.data.mongodb.core.mapping.Field("nestedValue2") String value2;
}
}