Skip to content

Commit 77699dd

Browse files
christophstroblmp911de
authored andcommitted
Validate aggregation query method on query method creation.
This commit makes sure to fail early if an annotated string based annotation does not contain a syntactically valid pipeline. Original pull request: spring-projects#4547 Closes spring-projects#4546
1 parent cd16375 commit 77699dd

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryMethod.java

+11
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.data.mongodb.repository.ReadPreference;
4040
import org.springframework.data.mongodb.repository.Tailable;
4141
import org.springframework.data.mongodb.repository.Update;
42+
import org.springframework.data.mongodb.util.BsonUtils;
4243
import org.springframework.data.projection.ProjectionFactory;
4344
import org.springframework.data.repository.core.RepositoryMetadata;
4445
import org.springframework.data.repository.query.QueryMethod;
@@ -504,6 +505,16 @@ public void verify() {
504505
}
505506
}
506507
}
508+
if (hasAnnotatedAggregation()) {
509+
for (String stage : getAnnotatedAggregation()) {
510+
if (BsonUtils.isJsonArray(stage)) {
511+
throw new IllegalStateException("""
512+
Invalid aggregation pipeline. Please split Aggregation.pipeline from "[{...}, {...}]" to "{...}", "{...}".
513+
Offending Method: %s.%s
514+
""".formatted(method.getDeclaringClass().getSimpleName(), method.getName()));
515+
}
516+
}
517+
}
507518
}
508519

509520
private boolean isNumericOrVoidReturnValue() {

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryMethodUnitTests.java

+15
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,15 @@ void detectsReadPreferenceForAggregation() throws Exception {
358358
assertThat(method.getAnnotatedReadPreference()).isEqualTo("secondaryPreferred");
359359
}
360360

361+
@Test // GH-4546
362+
void errorsOnInvalidAggregation() throws Exception {
363+
364+
assertThatExceptionOfType(IllegalStateException.class) //
365+
.isThrownBy(() -> queryMethod(InvalidAggregationMethodRepo.class, "findByAggregation").verify()) //
366+
.withMessageContaining("Invalid aggregation") //
367+
.withMessageContaining("findByAggregation");
368+
}
369+
361370
private MongoQueryMethod queryMethod(Class<?> repository, String name, Class<?>... parameters) throws Exception {
362371

363372
Method method = repository.getMethod(name, parameters);
@@ -465,6 +474,12 @@ interface InvalidUpdateMethodRepo extends Repository<Person, Long> {
465474
Person findAndIncrementVisitsByFirstname(String firstname);
466475
}
467476

477+
interface InvalidAggregationMethodRepo extends Repository<Person, Long> {
478+
479+
@Aggregation("[{'$group': { _id: '$templateId', maxVersion : { $max : '$version'} } }]")
480+
List<User> findByAggregation();
481+
}
482+
468483
interface Customer {
469484

470485
}

0 commit comments

Comments
 (0)