Browse Source

Fix NPE when traversing map.

We now use regular iteration instead of the Stream API.

Closes: #4567
Original pull request: #4568
pull/4616/head
Christoph Strobl 2 years ago committed by Mark Paluch
parent
commit
17e8837981
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 23
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java
  2. 12
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java

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

@ -617,15 +617,20 @@ public class QueryMapper { @@ -617,15 +617,20 @@ public class QueryMapper {
if (source instanceof Map<?,?> sourceMap) {
return sourceMap.entrySet().stream().collect(Collectors.toMap(
entry -> ObjectUtils.nullSafeToString(converter.convertToMongoType(entry.getKey())),
entry -> {
if (entry.getValue() instanceof Document document) {
return getMappedObject(document, entity);
}
return delegateConvertToMongoType(entry.getValue(), entity);
}
));
Map<String, Object> map = new LinkedHashMap<>(sourceMap.size(), 1F);
sourceMap.entrySet().forEach(it -> {
String key = ObjectUtils.nullSafeToString(converter.convertToMongoType(it.getKey()));
if (it.getValue() instanceof Document document) {
map.put(key, getMappedObject(document, entity));
} else {
map.put(key, delegateConvertToMongoType(it.getValue(), entity));
}
});
return map;
}
return delegateConvertToMongoType(source, entity);

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

@ -762,6 +762,18 @@ class UpdateMapperUnitTests { @@ -762,6 +762,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() {

Loading…
Cancel
Save