Browse Source

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

Original pull request: #491.
pull/491/merge
Christoph Strobl 9 years ago committed by Mark Paluch
parent
commit
e8ae928e74
  1. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java
  2. 10
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationUnitTests.java
  3. 23
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContextUnitTests.java

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

@ -1407,7 +1407,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation { @@ -1407,7 +1407,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) {

10
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationUnitTests.java

@ -559,6 +559,16 @@ public class AggregationUnitTests { @@ -559,6 +559,16 @@ public class AggregationUnitTests {
assertThat(getAsDocument(fields, "foosum"), isBsonObject().containing("$first.$cond.else", "no-answer"));
}
@Test // DATAMONGO-1756
public void projectOperationShouldRenderNestedFieldNamesCorrectly() {
Document agg = newAggregation(project().and("value1.value").plus("value2.value").as("val")).toDocument("collection",
Aggregation.DEFAULT_CONTEXT);
assertThat(extractPipelineElement(agg, 0, "$project"),
is(equalTo(new Document("val", new Document("$add", Arrays.asList("$value1.value", "$value2.value"))))));
}
private Document extractPipelineElement(Document agg, int index, String operation) {
List<Document> pipeline = (List<Document>) agg.get("pipeline");

23
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContextUnitTests.java

@ -338,6 +338,18 @@ public class TypeBasedAggregationOperationContextUnitTests { @@ -338,6 +338,18 @@ public class TypeBasedAggregationOperationContextUnitTests {
assertThat(age, isBsonObject().containing("$ifNull.[1]._class", Age.class.getName()));
}
@Test // DATAMONGO-1756
public void projectOperationShouldRenderNestedFieldNamesCorrectlyForTypedAggregation() {
AggregationOperationContext context = getContext(Wrapper.class);
Document agg = newAggregation(Wrapper.class, project().and("nested1.value1").plus("nested2.value2").as("val"))
.toDocument("collection", context);
assertThat(getPipelineElementFromAggregationAt(agg, 0).get("$project"), is(
equalTo(new Document("val", new Document("$add", Arrays.asList("$nested1.value1", "$field2.nestedValue2"))))));
}
@org.springframework.data.mongodb.core.mapping.Document(collection = "person")
public static class FooPerson {
@ -408,4 +420,15 @@ public class TypeBasedAggregationOperationContextUnitTests { @@ -408,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;
}
}

Loading…
Cancel
Save