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.
This commit is contained in:
committed by
Mark Paluch
parent
e91809957d
commit
bf606bcc47
@@ -163,9 +163,9 @@ abstract class AggregationUtils {
|
|||||||
* @throws IllegalArgumentException when none of the above rules is met.
|
* @throws IllegalArgumentException when none of the above rules is met.
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
static <T> T extractSimpleTypeResult(Document source, Class<T> targetType, MongoConverter converter) {
|
static <T> T extractSimpleTypeResult(@Nullable Document source, Class<T> targetType, MongoConverter converter) {
|
||||||
|
|
||||||
if (source.isEmpty()) {
|
if (source == null || source.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -159,6 +159,14 @@ public class StringBasedAggregationUnitTests {
|
|||||||
assertThat(executeAggregation("returnCollection").result).isEqualTo(expected);
|
assertThat(executeAggregation("returnCollection").result).isEqualTo(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // GH-3623
|
||||||
|
public void returnNullWhenSingleResultIsNotPresent() {
|
||||||
|
|
||||||
|
when(aggregationResults.getMappedResults()).thenReturn(Collections.emptyList());
|
||||||
|
|
||||||
|
assertThat(executeAggregation("simpleReturnType").result).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
@Test // DATAMONGO-2153
|
@Test // DATAMONGO-2153
|
||||||
public void returnRawResultType() {
|
public void returnRawResultType() {
|
||||||
assertThat(executeAggregation("returnRawResultType").result).isEqualTo(aggregationResults);
|
assertThat(executeAggregation("returnRawResultType").result).isEqualTo(aggregationResults);
|
||||||
@@ -312,6 +320,9 @@ public class StringBasedAggregationUnitTests {
|
|||||||
|
|
||||||
@Aggregation(RAW_GROUP_BY_LASTNAME_STRING)
|
@Aggregation(RAW_GROUP_BY_LASTNAME_STRING)
|
||||||
Page<Person> invalidPageReturnType(Pageable page);
|
Page<Person> invalidPageReturnType(Pageable page);
|
||||||
|
|
||||||
|
@Aggregation(RAW_GROUP_BY_LASTNAME_STRING)
|
||||||
|
String simpleReturnType();
|
||||||
}
|
}
|
||||||
|
|
||||||
static class PersonAggregate {
|
static class PersonAggregate {
|
||||||
|
|||||||
Reference in New Issue
Block a user