Fix NPE when rendering untyped aggregation operation as part of criteria query.

Resolves: #4687
Original pull request: #4695
This commit is contained in:
Christoph Strobl
2024-04-30 09:59:01 +02:00
committed by Mark Paluch
parent 0b4ca78084
commit c7800a6549
2 changed files with 14 additions and 2 deletions

View File

@@ -588,8 +588,11 @@ public class QueryMapper {
}
if (source instanceof AggregationExpression age) {
return age
.toDocument(new RelaxedTypeBasedAggregationOperationContext(entity.getType(), this.mappingContext, this));
if(entity == null) {
return age.toDocument();
}
return age.toDocument(new RelaxedTypeBasedAggregationOperationContext(entity.getType(), this.mappingContext, this));
}
if (source instanceof MongoExpression exr) {

View File

@@ -1529,6 +1529,15 @@ public class QueryMapperUnitTests {
assertThat(mappedObject).isEqualTo("{ $expr : { $expr : { $gt : [ '$foo', '$budget'] } } }");
}
@Test // GH-4687
void usageOfUntypedAggregationShouldRenderOperationsAsIs() {
Query query = query(expr(Expr.valueOf(ComparisonOperators.valueOf("field").greaterThan("budget"))));
org.bson.Document mappedObject = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(Object.class));
assertThat(mappedObject).isEqualTo("{ $expr : { $expr : { $gt : [ '$field', '$budget'] } } }");
}
@Test // GH-2750
void usesMongoExpressionDocumentAsIsIfItIsNotAnAggregationExpression() {