Skip to content

Fix NPE when traversing map #4568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.3.0-SNAPSHOT</version>
<version>4.3.x-4567-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.3.0-SNAPSHOT</version>
<version>4.3.x-4567-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.3.0-SNAPSHOT</version>
<version>4.3.x-4567-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.3.0-SNAPSHOT</version>
<version>4.3.x-4567-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,15 +617,20 @@ protected Object convertSimpleOrDocument(Object source, @Nullable MongoPersisten

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;

import org.bson.conversions.Bson;
import org.bson.types.Code;
Expand Down Expand Up @@ -1517,6 +1518,20 @@ void usesKeyNameWithDotsIfFieldNameTypeIsKey() {
assertThat(mappedObject).isEqualTo("{ 'field.name.with.dots' : 'A' }");
}

@Test // GH-4577
void mappingShouldRetainMapKeyOrder() {

TreeMap<String, String> sourceMap = new TreeMap<>(Map.of("test1", "123", "test2", "456"));

org.bson.Document target = mapper.getMappedObject(query(where("simpleMap").is(sourceMap)).getQueryObject(),
context.getPersistentEntity(WithSimpleMap.class));
assertThat(target.get("simpleMap", Map.class)).containsExactlyEntriesOf(sourceMap);
}

class WithSimpleMap {
Map<String, String> simpleMap;
}

class WithDeepArrayNesting {

List<WithNestedArray> level0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,18 @@ void mappingShouldNotContainTypeInformationWhenValueTypeOfMapMatchesDeclaration(
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() {
Expand Down