Skip to content

Commit e8ae928

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1756 - Fix nested field name resolution for arithmetic aggregation ops.
Original pull request: spring-projects#491.
1 parent f2e72fe commit e8ae928

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ public Document toDocument(AggregationOperationContext context) {
14071407
protected List<Object> getOperationArguments(AggregationOperationContext context) {
14081408

14091409
List<Object> result = new ArrayList<Object>(values.size());
1410-
result.add(context.getReference(getField().getName()).toString());
1410+
result.add(context.getReference(getField()).toString());
14111411

14121412
for (Object element : values) {
14131413

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

+10
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,16 @@ public void groupOperationShouldAllowUsageOfDerivedSpELAggregationExpression() {
559559
assertThat(getAsDocument(fields, "foosum"), isBsonObject().containing("$first.$cond.else", "no-answer"));
560560
}
561561

562+
@Test // DATAMONGO-1756
563+
public void projectOperationShouldRenderNestedFieldNamesCorrectly() {
564+
565+
Document agg = newAggregation(project().and("value1.value").plus("value2.value").as("val")).toDocument("collection",
566+
Aggregation.DEFAULT_CONTEXT);
567+
568+
assertThat(extractPipelineElement(agg, 0, "$project"),
569+
is(equalTo(new Document("val", new Document("$add", Arrays.asList("$value1.value", "$value2.value"))))));
570+
}
571+
562572
private Document extractPipelineElement(Document agg, int index, String operation) {
563573

564574
List<Document> pipeline = (List<Document>) agg.get("pipeline");

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

+23
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,18 @@ public void rendersAggregationIfNullInTypedAggregationContextCorrectly() {
338338
assertThat(age, isBsonObject().containing("$ifNull.[1]._class", Age.class.getName()));
339339
}
340340

341+
@Test // DATAMONGO-1756
342+
public void projectOperationShouldRenderNestedFieldNamesCorrectlyForTypedAggregation() {
343+
344+
AggregationOperationContext context = getContext(Wrapper.class);
345+
346+
Document agg = newAggregation(Wrapper.class, project().and("nested1.value1").plus("nested2.value2").as("val"))
347+
.toDocument("collection", context);
348+
349+
assertThat(getPipelineElementFromAggregationAt(agg, 0).get("$project"), is(
350+
equalTo(new Document("val", new Document("$add", Arrays.asList("$nested1.value1", "$field2.nestedValue2"))))));
351+
}
352+
341353
@org.springframework.data.mongodb.core.mapping.Document(collection = "person")
342354
public static class FooPerson {
343355

@@ -408,4 +420,15 @@ static class Bar {
408420

409421
String name;
410422
}
423+
424+
static class Wrapper {
425+
426+
Nested nested1;
427+
@org.springframework.data.mongodb.core.mapping.Field("field2") Nested nested2;
428+
}
429+
430+
static class Nested {
431+
String value1;
432+
@org.springframework.data.mongodb.core.mapping.Field("nestedValue2") String value2;
433+
}
411434
}

0 commit comments

Comments
 (0)