Skip to content

Commit bf606bc

Browse files
christophstroblmp911de
authored andcommitted
Fix NPE in declarative aggregation execution.
This commit fixes an issue where using a simple return type leads to NPE when the actual aggregation result does not contain any values. Closes: #3623 Original pull request: #3625.
1 parent e918099 commit bf606bc

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AggregationUtils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ static void appendLimitAndOffsetIfPresent(List<AggregationOperation> aggregation
163163
* @throws IllegalArgumentException when none of the above rules is met.
164164
*/
165165
@Nullable
166-
static <T> T extractSimpleTypeResult(Document source, Class<T> targetType, MongoConverter converter) {
166+
static <T> T extractSimpleTypeResult(@Nullable Document source, Class<T> targetType, MongoConverter converter) {
167167

168-
if (source.isEmpty()) {
168+
if (source == null || source.isEmpty()) {
169169
return null;
170170
}
171171

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/StringBasedAggregationUnitTests.java

+11
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ public void returnCollection() {
159159
assertThat(executeAggregation("returnCollection").result).isEqualTo(expected);
160160
}
161161

162+
@Test // GH-3623
163+
public void returnNullWhenSingleResultIsNotPresent() {
164+
165+
when(aggregationResults.getMappedResults()).thenReturn(Collections.emptyList());
166+
167+
assertThat(executeAggregation("simpleReturnType").result).isNull();
168+
}
169+
162170
@Test // DATAMONGO-2153
163171
public void returnRawResultType() {
164172
assertThat(executeAggregation("returnRawResultType").result).isEqualTo(aggregationResults);
@@ -312,6 +320,9 @@ private interface SampleRepository extends Repository<Person, Long> {
312320

313321
@Aggregation(RAW_GROUP_BY_LASTNAME_STRING)
314322
Page<Person> invalidPageReturnType(Pageable page);
323+
324+
@Aggregation(RAW_GROUP_BY_LASTNAME_STRING)
325+
String simpleReturnType();
315326
}
316327

317328
static class PersonAggregate {

0 commit comments

Comments
 (0)