Skip to content

Commit b07178e

Browse files
Expose output of SetWindowFieldsOperation correctly to next aggregation stage.
This commit makes sure to expose calculated output fields correctly.
1 parent ab9322b commit b07178e

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static SetWindowFieldsOperationBuilder builder() {
7070

7171
@Override
7272
public ExposedFields getFields() {
73-
return ExposedFields.nonSynthetic(Fields.from(output.fields.toArray(new Field[0])));
73+
return ExposedFields.synthetic(Fields.from(output.fields.toArray(new Field[0])));
7474
}
7575

7676
@Override

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,29 @@ void executesSetWindowFieldsOperationCorrectly() {
7373
238, 378);
7474
}
7575

76+
@Test // GH-4745
77+
void exposesFieldsToNextStageCorrectly() {
78+
79+
initCakeSales();
80+
81+
SetWindowFieldsOperation setWindowFieldsOperation = SetWindowFieldsOperation.builder() //
82+
.partitionByField("state") // resolves to field ref "$state"
83+
.sortBy(Sort.by(Direction.ASC, "date")) // resolves to "orderDate"
84+
.output(AccumulatorOperators.valueOf("qty").sum()) // resolves to "$quantity"
85+
.within(Windows.documents().fromUnbounded().toCurrent().build()) //
86+
.as("cumulativeQuantityForState") //
87+
.build(); //
88+
89+
AggregationResults<Document> results = mongoTemplate.aggregateAndReturn(Document.class)
90+
.by(Aggregation.newAggregation(CakeSale.class, setWindowFieldsOperation,
91+
/* and now project on the field to see it can be referenced */
92+
Aggregation.project("cumulativeQuantityForState")))
93+
.all();
94+
95+
assertThat(results.getMappedResults()).map(it -> it.get("cumulativeQuantityForState")).contains(162, 282, 427, 134,
96+
238, 378);
97+
}
98+
7699
@Test // GH-3711
77100
void executesSetWindowFieldsOperationWithPartitionExpressionCorrectly() {
78101

0 commit comments

Comments
 (0)