Skip to content

Commit 5197834

Browse files
Fix NPE when reading/mapping null value inside collection.
Closes: #3686
1 parent e1651fa commit 5197834

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -2006,12 +2006,16 @@ protected static class ConversionContext {
20062006
/**
20072007
* Converts a source object into {@link TypeInformation target}.
20082008
*
2009-
* @param source must not be {@literal null}.
2009+
* @param source can be {@literal null}.
20102010
* @param typeHint must not be {@literal null}.
20112011
* @return the converted object.
20122012
*/
20132013
@SuppressWarnings("unchecked")
2014-
public <S extends Object> S convert(Object source, TypeInformation<? extends S> typeHint) {
2014+
public <S extends Object> S convert(@Nullable Object source, TypeInformation<? extends S> typeHint) {
2015+
2016+
if(source == null) {
2017+
return null;
2018+
}
20152019

20162020
Assert.notNull(typeHint, "TypeInformation must not be null");
20172021

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

+39
Original file line numberDiff line numberDiff line change
@@ -2532,6 +2532,41 @@ void shouldWriteNullPropertyCorrectly() {
25322532
assertThat(document).containsEntry("writeAlwaysPerson", null).doesNotContainKey("writeNonNullPerson");
25332533
}
25342534

2535+
@Test // GH-3686
2536+
void readsCollectionContainingNullValue() {
2537+
2538+
org.bson.Document source = new org.bson.Document("items", Arrays.asList(new org.bson.Document("itemKey", "i1"), null, new org.bson.Document("itemKey", "i3")));
2539+
2540+
Order target = converter.read(Order.class, source);
2541+
2542+
assertThat(target.items)
2543+
.map(it -> it != null ? it.itemKey : null)
2544+
.containsExactly("i1", null, "i3");
2545+
}
2546+
2547+
@Test // GH-3686
2548+
void readsArrayContainingNullValue() {
2549+
2550+
org.bson.Document source = new org.bson.Document("arrayOfStrings", Arrays.asList("i1", null, "i3"));
2551+
2552+
WithArrays target = converter.read(WithArrays.class, source);
2553+
2554+
assertThat(target.arrayOfStrings).containsExactly("i1", null, "i3");
2555+
}
2556+
2557+
@Test // GH-3686
2558+
void readsMapContainingNullValue() {
2559+
2560+
org.bson.Document source = new org.bson.Document("mapOfObjects", new org.bson.Document("item1", "i1").append("item2", null).append("item3", "i3"));
2561+
2562+
ClassWithMapProperty target = converter.read(ClassWithMapProperty.class, source);
2563+
2564+
assertThat(target.mapOfObjects)
2565+
.containsEntry("item1", "i1")
2566+
.containsEntry("item2", null)
2567+
.containsEntry("item3", "i3");
2568+
}
2569+
25352570
static class GenericType<T> {
25362571
T content;
25372572
}
@@ -2893,6 +2928,10 @@ static class WithArrayInConstructor {
28932928

28942929
}
28952930

2931+
static class WithArrays {
2932+
String[] arrayOfStrings;
2933+
}
2934+
28962935
// DATAMONGO-1898
28972936

28982937
// DATACMNS-1278

0 commit comments

Comments
 (0)