Skip to content

Commit 512f81f

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: #4547 Closes #4546
1 parent bc18b52 commit 512f81f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.data.mongodb.repository.Query;
3939
import org.springframework.data.mongodb.repository.Tailable;
4040
import org.springframework.data.mongodb.repository.Update;
41+
import org.springframework.data.mongodb.util.BsonUtils;
4142
import org.springframework.data.projection.ProjectionFactory;
4243
import org.springframework.data.repository.core.RepositoryMetadata;
4344
import org.springframework.data.repository.query.QueryMethod;
@@ -456,6 +457,16 @@ public void verify() {
456457
}
457458
}
458459
}
460+
if (hasAnnotatedAggregation()) {
461+
for (String stage : getAnnotatedAggregation()) {
462+
if (BsonUtils.isJsonArray(stage)) {
463+
throw new IllegalStateException("""
464+
Invalid aggregation pipeline. Please split Aggregation.pipeline from "[{...}, {...}]" to "{...}", "{...}".
465+
Offending Method: %s.%s
466+
""".formatted(method.getDeclaringClass().getSimpleName(), method.getName()));
467+
}
468+
}
469+
}
459470
}
460471

461472
private boolean isNumericOrVoidReturnValue() {

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

+15
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,15 @@ void annotatedCollationClashSelectsAtCollationAnnotationValue() throws Exception
311311
assertThat(method.getAnnotatedCollation()).isEqualTo("de_AT");
312312
}
313313

314+
@Test // GH-4546
315+
void errorsOnInvalidAggregation() throws Exception {
316+
317+
assertThatExceptionOfType(IllegalStateException.class) //
318+
.isThrownBy(() -> queryMethod(InvalidAggregationMethodRepo.class, "findByAggregation").verify()) //
319+
.withMessageContaining("Invalid aggregation") //
320+
.withMessageContaining("findByAggregation");
321+
}
322+
314323
private MongoQueryMethod queryMethod(Class<?> repository, String name, Class<?>... parameters) throws Exception {
315324

316325
Method method = repository.getMethod(name, parameters);
@@ -404,6 +413,12 @@ interface InvalidUpdateMethodRepo extends Repository<Person, Long> {
404413
Person findAndIncrementVisitsByFirstname(String firstname);
405414
}
406415

416+
interface InvalidAggregationMethodRepo extends Repository<Person, Long> {
417+
418+
@Aggregation("[{'$group': { _id: '$templateId', maxVersion : { $max : '$version'} } }]")
419+
List<User> findByAggregation();
420+
}
421+
407422
interface Customer {
408423

409424
}

0 commit comments

Comments
 (0)