Skip to content

Validate String based aggregation on query method creation. #4547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4546-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4546-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4546-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4546-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.springframework.data.mongodb.repository.ReadPreference;
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 @@ -504,6 +505,16 @@ public void verify() {
}
}
}
if (hasAnnotatedAggregation()) {
for (String stage : getAnnotatedAggregation()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using ParameterBindingJsonReader to verify that each declaration contains a single object?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The QueryMethod itself does at this point not know about any parameters nor the parser to use so any validation attempt would be quite error prone due to the potential, non resolvable placeholders contained in the source string.
However there's BsonUtils#isJsonArray which we might want to reuse.

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 @@ -358,6 +358,15 @@ void detectsReadPreferenceForAggregation() throws Exception {
assertThat(method.getAnnotatedReadPreference()).isEqualTo("secondaryPreferred");
}

@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 @@ -465,6 +474,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