DATAMONGO-368 - MappingMongoConverter does not remove null values from collections anymore.
This commit is contained in:
@@ -499,13 +499,9 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
|
||||
for (Object element : source) {
|
||||
|
||||
if (element == null) {
|
||||
continue;
|
||||
}
|
||||
Class<?> elementType = element == null ? null : element.getClass();
|
||||
|
||||
Class<?> elementType = element.getClass();
|
||||
|
||||
if (conversions.isSimpleType(elementType)) {
|
||||
if (elementType == null || conversions.isSimpleType(elementType)) {
|
||||
sink.add(getPotentiallyConvertedSimpleWrite(element));
|
||||
} else if (element instanceof Collection || elementType.isArray()) {
|
||||
sink.add(writeCollectionInternal(asCollection(element), componentType, new BasicDBList()));
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
@@ -44,8 +45,6 @@ import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.PersistenceConstructor;
|
||||
import org.springframework.data.mongodb.MongoDbFactory;
|
||||
import org.springframework.data.mongodb.core.convert.CustomConversions;
|
||||
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
|
||||
import org.springframework.data.mongodb.core.mapping.Field;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
import org.springframework.data.mongodb.core.mapping.PersonPojoStringId;
|
||||
@@ -855,6 +854,25 @@ public class MappingMongoConverterUnitTests {
|
||||
assertThat(result.get("_id"), is((Object) 5));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-368
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writesNullValuesForCollection() {
|
||||
|
||||
CollectionWrapper wrapper = new CollectionWrapper();
|
||||
wrapper.contacts = Arrays.<Contact> asList(new Person(), null);
|
||||
|
||||
DBObject result = new BasicDBObject();
|
||||
converter.write(wrapper, result);
|
||||
|
||||
Object contacts = result.get("contacts");
|
||||
assertThat(contacts, is(Collection.class));
|
||||
assertThat(((Collection<?>) contacts).size(), is(2));
|
||||
assertThat(((Collection<Object>) contacts), hasItem(nullValue()));
|
||||
}
|
||||
|
||||
class GenericType<T> {
|
||||
T content;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user