Skip to content

Document list/map/set initialization on read. #4574

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 2 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-4571-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-4571-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-4571-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-4571-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ public static boolean hasValue(Bson bson, FieldName fieldName) {

Map<String, Object> source = asMap(bson);
if (fieldName.isKey()) {
return source.get(fieldName.name()) != null;
return source.containsKey(fieldName.name());
}

String[] parts = fieldName.parts();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Stream;

import org.bson.BsonUndefined;
import org.bson.types.Binary;
Expand All @@ -38,6 +40,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.Mockito;
Expand Down Expand Up @@ -673,6 +677,15 @@ void readsListOfMapsCorrectly() {
assertThat(wrapper.listOfMaps.get(0).get("Foo")).isEqualTo(Locale.ENGLISH);
}

@ParameterizedTest // GH-4571
@MethodSource("listMapSetReadingSource")
<T> void initializesListMapSetPropertiesIfRequiredOnRead(org.bson.Document source, Class<T> type,
Function<T, Object> valueFunction, Object expectedValue) {

T target = converter.read(type, source);
assertThat(target).extracting(valueFunction).isEqualTo(expectedValue);
}

@Test // DATAMONGO-259
void writesPlainMapOfCollectionsCorrectly() {

Expand Down Expand Up @@ -2986,6 +2999,49 @@ org.bson.Document write(Object source) {
return target;
}

private static Stream<Arguments> listMapSetReadingSource() {

Function<CollectionWrapper, Object> contacts = CollectionWrapper::getContacts;
Function<CollectionWrapper, Object> contactsSet = CollectionWrapper::getContactsSet;
Function<CollectionWrapper, Object> autoInitList = CollectionWrapper::getAutoInitList;
Function<ClassWithMapProperty, Object> map = ClassWithMapProperty::getMap;
Function<ClassWithMapProperty, Object> autoInitMap = ClassWithMapProperty::getAutoInitMap;

return Stream.of( //

// List
Arguments.of(new org.bson.Document("contacts", Collections.emptyList()), CollectionWrapper.class, contacts,
Collections.emptyList()),
Arguments.of(new org.bson.Document("contacts", null), CollectionWrapper.class, contacts, null),
Arguments.of(new org.bson.Document(), CollectionWrapper.class, contacts, null),

// ctor initialized List
Arguments.of(new org.bson.Document("autoInitList", Collections.emptyList()), CollectionWrapper.class,
autoInitList, Collections.emptyList()),
Arguments.of(new org.bson.Document("autoInitList", null), CollectionWrapper.class, autoInitList, null),
Arguments.of(new org.bson.Document(), CollectionWrapper.class, autoInitList,
Collections.singletonList("spring")),

// Set
Arguments.of(new org.bson.Document("contactsSet", Collections.emptyList()), CollectionWrapper.class,
contactsSet, Collections.emptySet()),
Arguments.of(new org.bson.Document("contactsSet", null), CollectionWrapper.class, contactsSet, null),
Arguments.of(new org.bson.Document(), CollectionWrapper.class, contactsSet, null),

// Map
Arguments.of(new org.bson.Document("map", new org.bson.Document()), ClassWithMapProperty.class, map,
Collections.emptyMap()),
Arguments.of(new org.bson.Document("map", null), ClassWithMapProperty.class, map, null),
Arguments.of(new org.bson.Document(), ClassWithMapProperty.class, map, null),

// ctor initialized Map
Arguments.of(new org.bson.Document("autoInitMap", new org.bson.Document()), ClassWithMapProperty.class,
autoInitMap, Collections.emptyMap()),
Arguments.of(new org.bson.Document("autoInitMap", null), ClassWithMapProperty.class, autoInitMap, null),
Arguments.of(new org.bson.Document(), ClassWithMapProperty.class, autoInitMap,
Collections.singletonMap("spring", "data")));
}

static class GenericType<T> {
T content;
}
Expand Down Expand Up @@ -3142,11 +3198,20 @@ static class ClassWithSortedMap {

static class ClassWithMapProperty {
Map<Locale, String> map;
Map<String, String> autoInitMap = Collections.singletonMap("spring", "data");
Map<String, List<String>> mapOfLists;
Map<String, Object> mapOfObjects;
Map<String, String[]> mapOfStrings;
Map<String, Person> mapOfPersons;
TreeMap<String, Person> treeMapOfPersons;

public Map<Locale, String> getMap() {
return map;
}

public Map<String, String> getAutoInitMap() {
return this.autoInitMap;
}
}

static class ClassWithNestedMaps {
Expand All @@ -3168,6 +3233,19 @@ static class CollectionWrapper {
List<List<String>> strings;
List<Map<String, Locale>> listOfMaps;
Set<Contact> contactsSet;
List<String> autoInitList = Collections.singletonList("spring");

public List<Contact> getContacts() {
return contacts;
}

public Set<Contact> getContactsSet() {
return contactsSet;
}

public List<String> getAutoInitList() {
return autoInitList;
}
}

static class LocaleWrapper {
Expand Down
11 changes: 11 additions & 0 deletions src/main/antora/modules/ROOT/pages/mongodb/mapping/mapping.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@ calling `get()` before the actual conversion
|===
====

.Collection Handling
[NOTE]
====
Collection handing depends on the actual values retrieved from the MongoDB.

* If a document does **not** contain the field mapped to a collection, the mapping will not touch the property.
Which means the value will remain `null`, a java default or any value set during object creation.
* If the document contains the field to be mapped, but the field holds a `null` value (like: `{ 'list' : null }`), the property value is set to `null` overriding any default value set during object creation.
* If the document contains the field to be mapped to a collection which is **not** `null` (like: `{ 'list' : [ ... ] }`), the collection is populated with the mapped values overriding any default value set during object creation.
====

[[mapping-configuration]]
== Mapping Configuration

Expand Down