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
This commit is contained in:
committed by
Mark Paluch
parent
cd16375fea
commit
77699ddd50
@@ -39,6 +39,7 @@ import org.springframework.data.mongodb.repository.Query;
|
||||
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;
|
||||
@@ -504,6 +505,16 @@ public class MongoQueryMethod extends QueryMethod {
|
||||
}
|
||||
}
|
||||
}
|
||||
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() {
|
||||
|
||||
@@ -358,6 +358,15 @@ public class MongoQueryMethodUnitTests {
|
||||
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);
|
||||
@@ -465,6 +474,12 @@ public class MongoQueryMethodUnitTests {
|
||||
Person findAndIncrementVisitsByFirstname(String firstname);
|
||||
}
|
||||
|
||||
interface InvalidAggregationMethodRepo extends Repository<Person, Long> {
|
||||
|
||||
@Aggregation("[{'$group': { _id: '$templateId', maxVersion : { $max : '$version'} } }]")
|
||||
List<User> findByAggregation();
|
||||
}
|
||||
|
||||
interface Customer {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user