Skip to content

Commit 3dccdd2

Browse files
christophstroblmp911de
authored andcommitted
Fix NPE when traversing map.
We now use regular iteration instead of the Stream API. Closes: #4567 Original pull request: #4568
1 parent e20d12f commit 3dccdd2

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -617,15 +617,20 @@ protected Object convertSimpleOrDocument(Object source, @Nullable MongoPersisten
617617

618618
if (source instanceof Map<?,?> sourceMap) {
619619

620-
return sourceMap.entrySet().stream().collect(Collectors.toMap(
621-
entry -> ObjectUtils.nullSafeToString(converter.convertToMongoType(entry.getKey())),
622-
entry -> {
623-
if (entry.getValue() instanceof Document document) {
624-
return getMappedObject(document, entity);
625-
}
626-
return delegateConvertToMongoType(entry.getValue(), entity);
627-
}
628-
));
620+
Map<String, Object> map = new LinkedHashMap<>(sourceMap.size(), 1F);
621+
622+
sourceMap.entrySet().forEach(it -> {
623+
624+
String key = ObjectUtils.nullSafeToString(converter.convertToMongoType(it.getKey()));
625+
626+
if (it.getValue() instanceof Document document) {
627+
map.put(key, getMappedObject(document, entity));
628+
} else {
629+
map.put(key, delegateConvertToMongoType(it.getValue(), entity));
630+
}
631+
});
632+
633+
return map;
629634
}
630635

631636
return delegateConvertToMongoType(source, entity);

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,18 @@ void mappingShouldNotContainTypeInformationWhenValueTypeOfMapMatchesDeclaration(
762762
assertThat(mappedUpdate).doesNotContainKey("$set.concreteMap.jasnah._class");
763763
}
764764

765+
@Test // GH-4567
766+
void updateShouldAllowNullValuesInMap() {
767+
768+
Map<Object, NestedDocument> map = Collections.singletonMap("jasnah", new NestedDocument("kholin"));
769+
770+
Update update = new Update().set("concreteMap", Collections.singletonMap("jasnah", null));
771+
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
772+
context.getPersistentEntity(EntityWithObjectMap.class));
773+
774+
assertThat(mappedUpdate).isEqualTo(new Document("$set", new Document("concreteMap", Collections.singletonMap("jasnah", null))));
775+
}
776+
765777
@Test // DATAMONGO-1250
766778
@SuppressWarnings("unchecked")
767779
void mapsUpdateWithBothReadingAndWritingConverterRegistered() {

0 commit comments

Comments
 (0)