Skip to content

Commit c7800a6

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 0b4ca78 commit c7800a6

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
@@ -588,8 +588,11 @@ protected Object convertSimpleOrDocument(Object source, @Nullable MongoPersisten
588588
}
589589

590590
if (source instanceof AggregationExpression age) {
591-
return age
592-
.toDocument(new RelaxedTypeBasedAggregationOperationContext(entity.getType(), this.mappingContext, this));
591+
592+
if(entity == null) {
593+
return age.toDocument();
594+
}
595+
return age.toDocument(new RelaxedTypeBasedAggregationOperationContext(entity.getType(), this.mappingContext, this));
593596
}
594597

595598
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
@@ -1529,6 +1529,15 @@ void usageOfMongoExpressionOnCriteriaDoesNotUnwrapAnExprAggregationExpression()
15291529
assertThat(mappedObject).isEqualTo("{ $expr : { $expr : { $gt : [ '$foo', '$budget'] } } }");
15301530
}
15311531

1532+
@Test // GH-4687
1533+
void usageOfUntypedAggregationShouldRenderOperationsAsIs() {
1534+
1535+
Query query = query(expr(Expr.valueOf(ComparisonOperators.valueOf("field").greaterThan("budget"))));
1536+
org.bson.Document mappedObject = mapper.getMappedObject(query.getQueryObject(),
1537+
context.getPersistentEntity(Object.class));
1538+
assertThat(mappedObject).isEqualTo("{ $expr : { $expr : { $gt : [ '$field', '$budget'] } } }");
1539+
}
1540+
15321541
@Test // GH-2750
15331542
void usesMongoExpressionDocumentAsIsIfItIsNotAnAggregationExpression() {
15341543

0 commit comments

Comments
 (0)