DATADOC-228 - MappingMongoConverter now writes null for map values.

Fixes a NPE that one got when trying to persist a Map containing null as value for entries.
This commit is contained in:
Oliver Gierke
2011-07-28 11:17:47 +02:00
parent f349f5ea10
commit fd7e41b753
2 changed files with 18 additions and 1 deletions

View File

@@ -568,7 +568,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
// Don't use conversion service here as removal of ObjectToString converter results in some primitive types not
// being convertable
String simpleKey = key.toString();
if (conversions.isSimpleType(val.getClass())) {
if (val == null || conversions.isSimpleType(val.getClass())) {
writeSimpleInternal(simpleKey, val, dbo);
} else {
DBObject newDbo = new BasicDBObject();

View File

@@ -491,6 +491,23 @@ public class MappingMongoConverterUnitTests {
}
/**
* @see DATADOC-228
*/
@Test
public void writesNullValuesForMaps() {
ClassWithMapProperty foo = new ClassWithMapProperty();
foo.map = Collections.singletonMap(Locale.US, null);
DBObject result = new BasicDBObject();
converter.write(foo, result);
Object map = result.get("map");
assertThat(map, is(instanceOf(DBObject.class)));
assertThat(((DBObject) map).keySet(), hasItem("en_US"));
}
class GenericType<T> {
T content;
}