Skip to content

Commit 785eabf

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 0491ec3 commit 785eabf

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -607,11 +607,11 @@ protected Object convertSimpleOrDocument(Object source, @Nullable MongoPersisten
607607
return source;
608608
}
609609

610-
if (source instanceof Map) {
610+
if (source instanceof Map<?,?> sourceMap) {
611611

612-
Map<String, Object> map = new LinkedHashMap<>();
612+
Map<String, Object> map = new LinkedHashMap<>(sourceMap.size(), 1F);
613613

614-
((Map<String, Object>) source).entrySet().forEach(it -> {
614+
sourceMap.entrySet().forEach(it -> {
615615

616616
String key = ObjectUtils.nullSafeToString(converter.convertToMongoType(it.getKey()));
617617

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

+12
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,18 @@ void mappingShouldNotContainTypeInformationWhenValueTypeOfMapMatchesDeclaration(
768768
assertThat(mappedUpdate).doesNotContainKey("$set.concreteMap.jasnah._class");
769769
}
770770

771+
@Test // GH-4567
772+
void updateShouldAllowNullValuesInMap() {
773+
774+
Map<Object, NestedDocument> map = Collections.singletonMap("jasnah", new NestedDocument("kholin"));
775+
776+
Update update = new Update().set("concreteMap", Collections.singletonMap("jasnah", null));
777+
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
778+
context.getPersistentEntity(EntityWithObjectMap.class));
779+
780+
assertThat(mappedUpdate).isEqualTo(new Document("$set", new Document("concreteMap", Collections.singletonMap("jasnah", null))));
781+
}
782+
771783
@Test // DATAMONGO-1250
772784
@SuppressWarnings("unchecked")
773785
void mapsUpdateWithBothReadingAndWritingConverterRegistered() {

0 commit comments

Comments
 (0)