Skip to content

Commit 0c50d97

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

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ protected Object readCollectionOrArray(ConversionContext context, Collection<?>
11361136
}
11371137

11381138
for (Object element : source) {
1139-
items.add(context.convert(element, componentType));
1139+
items.add(element != null ? context.convert(element, componentType) : element);
11401140
}
11411141

11421142
return getPotentiallyConvertedSimpleRead(items, targetType.getType());
@@ -1880,6 +1880,7 @@ protected static class ConversionContext {
18801880
@SuppressWarnings("unchecked")
18811881
public <S extends Object> S convert(Object source, TypeInformation<? extends S> typeHint) {
18821882

1883+
Assert.notNull(source, "Source must not be null");
18831884
Assert.notNull(typeHint, "TypeInformation must not be null");
18841885

18851886
if (conversions.hasCustomReadTarget(source.getClass(), typeHint.getType())) {

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

+39
Original file line numberDiff line numberDiff line change
@@ -2520,6 +2520,41 @@ void usesCustomConverterForPropertiesUsingTypesImplementingMapOnRead() {
25202520
assertThat(target.typeImplementingMap).isEqualTo(new TypeImplementingMap("one", 2));
25212521
}
25222522

2523+
@Test // GH-3686
2524+
void readsCollectionContainingNullValue() {
2525+
2526+
org.bson.Document source = new org.bson.Document("items", Arrays.asList(new org.bson.Document("itemKey", "i1"), null, new org.bson.Document("itemKey", "i3")));
2527+
2528+
Order target = converter.read(Order.class, source);
2529+
2530+
assertThat(target.items)
2531+
.map(it -> it != null ? it.itemKey : null)
2532+
.containsExactly("i1", null, "i3");
2533+
}
2534+
2535+
@Test // GH-3686
2536+
void readsArrayContainingNullValue() {
2537+
2538+
org.bson.Document source = new org.bson.Document("arrayOfStrings", Arrays.asList("i1", null, "i3"));
2539+
2540+
WithArrays target = converter.read(WithArrays.class, source);
2541+
2542+
assertThat(target.arrayOfStrings).containsExactly("i1", null, "i3");
2543+
}
2544+
2545+
@Test // GH-3686
2546+
void readsMapContainingNullValue() {
2547+
2548+
org.bson.Document source = new org.bson.Document("mapOfObjects", new org.bson.Document("item1", "i1").append("item2", null).append("item3", "i3"));
2549+
2550+
ClassWithMapProperty target = converter.read(ClassWithMapProperty.class, source);
2551+
2552+
assertThat(target.mapOfObjects)
2553+
.containsEntry("item1", "i1")
2554+
.containsEntry("item2", null)
2555+
.containsEntry("item3", "i3");
2556+
}
2557+
25232558
static class GenericType<T> {
25242559
T content;
25252560
}
@@ -2881,6 +2916,10 @@ static class WithArrayInConstructor {
28812916

28822917
}
28832918

2919+
static class WithArrays {
2920+
String[] arrayOfStrings;
2921+
}
2922+
28842923
// DATAMONGO-1898
28852924

28862925
// DATACMNS-1278

0 commit comments

Comments
 (0)