Skip to content

Commit d62639d

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 b13e47d commit d62639d

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
@@ -196,9 +196,9 @@ static void appendLimitAndOffsetIfPresent(List<AggregationOperation> aggregation
196196
* @throws IllegalArgumentException when none of the above rules is met.
197197
*/
198198
@Nullable
199-
static <T> T extractSimpleTypeResult(Document source, Class<T> targetType, MongoConverter converter) {
199+
static <T> T extractSimpleTypeResult(@Nullable Document source, Class<T> targetType, MongoConverter converter) {
200200

201-
if (source.isEmpty()) {
201+
if (source == null || source.isEmpty()) {
202202
return null;
203203
}
204204

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

+11
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ public void returnCollection() {
156156
assertThat(executeAggregation("returnCollection").result).isEqualTo(expected);
157157
}
158158

159+
@Test // GH-3623
160+
public void returnNullWhenSingleResultIsNotPresent() {
161+
162+
when(aggregationResults.getMappedResults()).thenReturn(Collections.emptyList());
163+
164+
assertThat(executeAggregation("simpleReturnType").result).isNull();
165+
}
166+
159167
@Test // DATAMONGO-2153
160168
public void returnRawResultType() {
161169
assertThat(executeAggregation("returnRawResultType").result).isEqualTo(aggregationResults);
@@ -299,6 +307,9 @@ private interface SampleRepository extends Repository<Person, Long> {
299307

300308
@Aggregation(RAW_GROUP_BY_LASTNAME_STRING)
301309
Page<Person> invalidPageReturnType(Pageable page);
310+
311+
@Aggregation(RAW_GROUP_BY_LASTNAME_STRING)
312+
String simpleReturnType();
302313
}
303314

304315
static class PersonAggregate {

0 commit comments

Comments
 (0)