Skip to content

Commit

Permalink
Validate aggregation query method on query method creation.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
christophstrobl authored and mp911de committed Nov 6, 2023
1 parent bc18b52 commit 512f81f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.mongodb.repository.Tailable;
import org.springframework.data.mongodb.repository.Update;
import org.springframework.data.mongodb.util.BsonUtils;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.query.QueryMethod;
Expand Down Expand Up @@ -456,6 +457,16 @@ public void verify() {
}
}
}
if (hasAnnotatedAggregation()) {
for (String stage : getAnnotatedAggregation()) {
if (BsonUtils.isJsonArray(stage)) {
throw new IllegalStateException("""
Invalid aggregation pipeline. Please split Aggregation.pipeline from "[{...}, {...}]" to "{...}", "{...}".
Offending Method: %s.%s
""".formatted(method.getDeclaringClass().getSimpleName(), method.getName()));
}
}
}
}

private boolean isNumericOrVoidReturnValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,15 @@ void annotatedCollationClashSelectsAtCollationAnnotationValue() throws Exception
assertThat(method.getAnnotatedCollation()).isEqualTo("de_AT");
}

@Test // GH-4546
void errorsOnInvalidAggregation() throws Exception {

assertThatExceptionOfType(IllegalStateException.class) //
.isThrownBy(() -> queryMethod(InvalidAggregationMethodRepo.class, "findByAggregation").verify()) //
.withMessageContaining("Invalid aggregation") //
.withMessageContaining("findByAggregation");
}

private MongoQueryMethod queryMethod(Class<?> repository, String name, Class<?>... parameters) throws Exception {

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

interface InvalidAggregationMethodRepo extends Repository<Person, Long> {

@Aggregation("[{'$group': { _id: '$templateId', maxVersion : { $max : '$version'} } }]")
List<User> findByAggregation();
}

interface Customer {

}
Expand Down

0 comments on commit 512f81f

Please sign in to comment.