Fix NPE when traversing map.

We now use regular iteration instead of the Stream API.

Closes: #4567
Original pull request: #4568
This commit is contained in:
Christoph Strobl
2023-11-24 14:48:39 +01:00
committed by Mark Paluch
parent 0491ec3ba5
commit 785eabface
2 changed files with 15 additions and 3 deletions

View File

@@ -607,11 +607,11 @@ public class QueryMapper {
return source;
}
if (source instanceof Map) {
if (source instanceof Map<?,?> sourceMap) {
Map<String, Object> map = new LinkedHashMap<>();
Map<String, Object> map = new LinkedHashMap<>(sourceMap.size(), 1F);
((Map<String, Object>) source).entrySet().forEach(it -> {
sourceMap.entrySet().forEach(it -> {
String key = ObjectUtils.nullSafeToString(converter.convertToMongoType(it.getKey()));

View File

@@ -768,6 +768,18 @@ class UpdateMapperUnitTests {
assertThat(mappedUpdate).doesNotContainKey("$set.concreteMap.jasnah._class");
}
@Test // GH-4567
void updateShouldAllowNullValuesInMap() {
Map<Object, NestedDocument> map = Collections.singletonMap("jasnah", new NestedDocument("kholin"));
Update update = new Update().set("concreteMap", Collections.singletonMap("jasnah", null));
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(EntityWithObjectMap.class));
assertThat(mappedUpdate).isEqualTo(new Document("$set", new Document("concreteMap", Collections.singletonMap("jasnah", null))));
}
@Test // DATAMONGO-1250
@SuppressWarnings("unchecked")
void mapsUpdateWithBothReadingAndWritingConverterRegistered() {