DATAMONGO-1843 - Fix parameter shadowing in ArrayOperators reduce.
Original pull request: #526.
This commit is contained in:
committed by
Mark Paluch
parent
95def1f0aa
commit
d3a54e7fe0
@@ -229,8 +229,8 @@ public class ArrayOperators {
|
||||
|
||||
@Override
|
||||
public Reduce startingWith(Object initialValue) {
|
||||
return (usesFieldRef() ? Reduce.arrayOf(fieldReference) : Reduce.arrayOf(expression))
|
||||
.withInitialValue(initialValue).reduce(expression);
|
||||
return (usesFieldRef() ? Reduce.arrayOf(fieldReference)
|
||||
: Reduce.arrayOf(ArrayOperatorFactory.this.expression)).withInitialValue(initialValue).reduce(expression);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1104,12 +1104,10 @@ public class ArrayOperators {
|
||||
/**
|
||||
* Start creating new {@link Reduce}.
|
||||
*
|
||||
* @param expression must not be {@literal null}.
|
||||
* @param arrayValueExpression must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static InitialValueBuilder arrayOf(final AggregationExpression expression) {
|
||||
|
||||
Assert.notNull(expression, "AggregationExpression must not be null");
|
||||
public static InitialValueBuilder arrayOf(final AggregationExpression arrayValueExpression) {
|
||||
|
||||
return new InitialValueBuilder() {
|
||||
|
||||
@@ -1124,14 +1122,14 @@ public class ArrayOperators {
|
||||
public Reduce reduce(AggregationExpression expression) {
|
||||
|
||||
Assert.notNull(expression, "AggregationExpression must not be null");
|
||||
return new Reduce(expression, initialValue, Collections.singletonList(expression));
|
||||
return new Reduce(arrayValueExpression, initialValue, Collections.singletonList(expression));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reduce reduce(PropertyExpression... expressions) {
|
||||
|
||||
Assert.notNull(expressions, "PropertyExpressions must not be null");
|
||||
return new Reduce(expression, initialValue, Arrays.<AggregationExpression> asList(expressions));
|
||||
return new Reduce(arrayValueExpression, initialValue, Arrays.asList(expressions));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -32,10 +32,13 @@ import org.hamcrest.Matchers;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.domain.Range;
|
||||
import org.springframework.data.mongodb.core.DocumentTestUtils;
|
||||
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce;
|
||||
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.PropertyExpression;
|
||||
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.Variable;
|
||||
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Slice;
|
||||
import org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Switch.CaseOperator;
|
||||
import org.springframework.data.mongodb.core.aggregation.ProjectionOperation.ProjectionOperationBuilder;
|
||||
import org.springframework.data.mongodb.core.aggregation.StringOperators.Concat;
|
||||
import org.springframework.data.mongodb.core.aggregation.VariableOperators.Let.ExpressionVariable;
|
||||
|
||||
/**
|
||||
@@ -1573,6 +1576,27 @@ public class ProjectionOperationUnitTests {
|
||||
"{ $project : { \"results\": { $reduce: { input: \"$probabilityArr\", initialValue: { \"sum\" : 5 , \"product\" : 2} , in: { \"sum\": { $add : [\"$$value.sum\", \"$$this\"] }, \"product\": { $multiply: [ \"$$value.product\", \"$$this\" ] } } } } } }")));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1843
|
||||
public void shouldRenderReduceWithInputAndInExpressionsCorrectly() {
|
||||
|
||||
Document exprected = Document.parse(
|
||||
"{ \"$project\" : { \"results\" : { \"$reduce\" : { \"input\" : { \"$slice\" : [\"$array\", 5] }, \"initialValue\" : \"\", \"in\" : { \"$concat\" : [\"$$value\", \"/\", \"$$this\"] } } } } }");
|
||||
|
||||
Reduce reduceEntryPoint = Reduce.arrayOf(Slice.sliceArrayOf("array").itemCount(5)) //
|
||||
.withInitialValue("") //
|
||||
.reduce(Concat.valueOf("$$value").concat("/").concatValueOf("$$this"));
|
||||
|
||||
Reduce arrayEntryPoint = ArrayOperators.arrayOf(Slice.sliceArrayOf("array").itemCount(5)) //
|
||||
.reduce(Concat.valueOf("$$value").concat("/").concatValueOf("$$this")) //
|
||||
.startingWith("");
|
||||
|
||||
assertThat(project().and(reduceEntryPoint).as("results").toDocument(Aggregation.DEFAULT_CONTEXT),
|
||||
Matchers.is(exprected));
|
||||
|
||||
assertThat(project().and(arrayEntryPoint).as("results").toDocument(Aggregation.DEFAULT_CONTEXT),
|
||||
Matchers.is(exprected));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1548
|
||||
public void shouldRenderZipCorrectly() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user