Skip to content

Commit 434a8e6

Browse files
christophstroblmp911de
authored andcommitted
Avoid multiple converter calls when parsing string aggregation stages.
Parsing of string based aggregations lead to multiple invocations of potential converters due to missing reuse of bound parameter value as well as attempts to verify out/merge stages within the pipeline that triggered the stage to be converted into the target document. The changes in this commit, reduce the number down to 2. One for examining potential expression dependencies and one for the actual conversion and parameter binding. See #4712 Original pull request: #4717
1 parent b10905a commit 434a8e6

File tree

5 files changed

+115
-7
lines changed

5 files changed

+115
-7
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ protected List<AggregationOperation> parseAggregationPipeline(String[] sourcePip
323323
}
324324

325325
private AggregationOperation computePipelineStage(String source, ConvertingParameterAccessor accessor) {
326-
return ctx -> ctx.getMappedObject(bindParameters(source, accessor), getQueryMethod().getDomainClass());
326+
return new StringAggregationOperation(source, getQueryMethod().getDomainClass(),
327+
(it) -> bindParameters(it, accessor));
327328
}
328329

329330
protected Document decode(String source, ParameterBindingContext bindingContext) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractReactiveMongoQuery.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,8 @@ protected Mono<List<AggregationOperation>> parseAggregationPipeline(String[] pip
376376
private Mono<AggregationOperation> computePipelineStage(String source, MongoParameterAccessor accessor,
377377
ParameterBindingDocumentCodec codec) {
378378

379-
return expressionEvaluator(source, accessor, codec).map(it -> {
380-
return ctx -> ctx.getMappedObject(decode(it.getT1(), source, accessor, it.getT2()),
381-
getQueryMethod().getDomainClass());
382-
});
379+
return expressionEvaluator(source, accessor, codec).map(
380+
it -> new StringAggregationOperation(source, AbstractReactiveMongoQuery.this.getQueryMethod().getDomainClass(), bsonString -> AbstractReactiveMongoQuery.this.decode(it.getT1(), bsonString, accessor, it.getT2())));
383381
}
384382

385383
private Mono<Tuple2<SpELExpressionEvaluator, ParameterBindingDocumentCodec>> expressionEvaluator(String source,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.repository.query;
17+
18+
import java.util.function.Function;
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
21+
22+
import org.bson.Document;
23+
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
24+
import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext;
25+
26+
/**
27+
* @author Christoph Strobl
28+
*/
29+
class StringAggregationOperation implements AggregationOperation {
30+
31+
private static final Pattern OPERATOR_PATTERN = Pattern.compile("\\$\\w+");
32+
33+
private final String source;
34+
private final Class<?> domainType;
35+
private final Function<String, Document> bindFunction;
36+
37+
StringAggregationOperation(String source, Class<?> domainType, Function<String, Document> bindFunction) {
38+
39+
this.source = source;
40+
this.domainType = domainType;
41+
this.bindFunction = bindFunction;
42+
}
43+
44+
@Override
45+
public Document toDocument(AggregationOperationContext context) {
46+
return context.getMappedObject(bindFunction.apply(source), domainType);
47+
}
48+
49+
@Override
50+
public String getOperator() {
51+
52+
Matcher matcher = OPERATOR_PATTERN.matcher(source);
53+
if (matcher.find()) {
54+
return matcher.group();
55+
}
56+
return AggregationOperation.super.getOperator();
57+
}
58+
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/json/ParameterBindingJsonReader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,9 @@ private BindableValue bindableValueFor(JsonToken token) {
401401
if (matcher.find()) {
402402

403403
int index = computeParameterIndex(matcher.group());
404-
bindableValue.setValue(getBindableValueForIndex(index));
405-
bindableValue.setType(bsonTypeForValue(getBindableValueForIndex(index)));
404+
Object bindableValueForIndex = getBindableValueForIndex(index);
405+
bindableValue.setValue(bindableValueForIndex);
406+
bindableValue.setType(bsonTypeForValue(bindableValueForIndex));
406407
return bindableValue;
407408
}
408409

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.repository.query;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import org.assertj.core.api.Assertions;
21+
import org.bson.Document;
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.params.ParameterizedTest;
24+
import org.junit.jupiter.params.provider.ValueSource;
25+
26+
/**
27+
* @author Christoph Strobl
28+
*/
29+
public class StringBasedAggregationOperationUnitTests {
30+
31+
@ParameterizedTest // GH-4712
32+
@ValueSource(strings = { "$project", "'$project'", "\"$project\"" })
33+
void extractsAggregationOperatorFromAggregationStringWithoutBindingParameters(String operator) {
34+
35+
StringAggregationOperation agg = new StringAggregationOperation("{ %s : { 'fn' : 1 } }".formatted(operator),
36+
Object.class, (it) -> Assertions.fail("o_O Parameter binding"));
37+
38+
assertThat(agg.getOperator()).isEqualTo("$project");
39+
}
40+
41+
@Test
42+
// GH-4712
43+
void fallbackToParameterBindingIfAggregationOperatorCannotBeExtractedFromAggregationStringWithoutBindingParameters() {
44+
45+
StringAggregationOperation agg = new StringAggregationOperation("{ happy-madison : { 'fn' : 1 } }", Object.class,
46+
(it) -> new Document("$project", ""));
47+
48+
assertThat(agg.getOperator()).isEqualTo("$project");
49+
}
50+
}

0 commit comments

Comments
 (0)