Skip to content

Commit cdd4a01

Browse files
christophstroblmp911de
authored andcommitted
Fix NPE when rendering untyped aggregation operation as part of criteria query.
Resolves: #4687 Original pull request: #4695
1 parent 27c812e commit cdd4a01

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,11 @@ protected Object convertSimpleOrDocument(Object source, @Nullable MongoPersisten
548548
}
549549

550550
if (source instanceof AggregationExpression age) {
551-
return age
552-
.toDocument(new RelaxedTypeBasedAggregationOperationContext(entity.getType(), this.mappingContext, this));
551+
552+
if(entity == null) {
553+
return age.toDocument();
554+
}
555+
return age.toDocument(new RelaxedTypeBasedAggregationOperationContext(entity.getType(), this.mappingContext, this));
553556
}
554557

555558
if (source instanceof MongoExpression exr) {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

+9
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,15 @@ void usageOfMongoExpressionOnCriteriaDoesNotUnwrapAnExprAggregationExpression()
16041604
assertThat(mappedObject).isEqualTo("{ $expr : { $expr : { $gt : [ '$foo', '$budget'] } } }");
16051605
}
16061606

1607+
@Test // GH-4687
1608+
void usageOfUntypedAggregationShouldRenderOperationsAsIs() {
1609+
1610+
Query query = query(expr(Expr.valueOf(ComparisonOperators.valueOf("field").greaterThan("budget"))));
1611+
org.bson.Document mappedObject = mapper.getMappedObject(query.getQueryObject(),
1612+
context.getPersistentEntity(Object.class));
1613+
assertThat(mappedObject).isEqualTo("{ $expr : { $expr : { $gt : [ '$field', '$budget'] } } }");
1614+
}
1615+
16071616
@Test // GH-2750
16081617
void usesMongoExpressionDocumentAsIsIfItIsNotAnAggregationExpression() {
16091618

0 commit comments

Comments
 (0)