DATADOC-259 - Fixed potential NullPointerException in MapingMongoConverter.writeMapInternal(…).

MappingMongoConverter.writeInternal(…) invoked ….writeMapInternal(…) handing in null for the TypeInformation which violated the implicit contract for the method. Made contract explicit in Javadoc and hand in plain Map TypeInformation.
This commit is contained in:
Oliver Gierke
2011-09-07 11:24:50 +02:00
parent 8b7521a93b
commit c4cd074d4d
2 changed files with 31 additions and 2 deletions

View File

@@ -75,6 +75,8 @@ import org.springframework.util.StringUtils;
public class MappingMongoConverter extends AbstractMongoConverter implements ApplicationContextAware,
TypeMapperProvider {
@SuppressWarnings("rawtypes")
private static final TypeInformation<Map> MAP_TYPE_INFORMATION = ClassTypeInformation.from(Map.class);
private static final List<Class<?>> VALID_ID_TYPES = Arrays.asList(new Class<?>[] { ObjectId.class, String.class,
BigInteger.class, byte[].class });
@@ -325,7 +327,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
}
if (Map.class.isAssignableFrom(obj.getClass())) {
writeMapInternal((Map<Object, Object>) obj, dbo, null);
writeMapInternal((Map<Object, Object>) obj, dbo, MAP_TYPE_INFORMATION);
return;
}
@@ -552,6 +554,13 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
return dbList;
}
/**
* Writes the given {@link Map} to the given {@link DBObject} considering the given {@link TypeInformation}.
*
* @param obj must not be {@literal null}.
* @param dbo must not be {@literal null}.
* @param propertyType must not be {@literal null}.
*/
protected void writeMapInternal(Map<Object, Object> obj, DBObject dbo, TypeInformation<?> propertyType) {
for (Map.Entry<Object, Object> entry : obj.entrySet()) {
Object key = entry.getKey();
@@ -586,7 +595,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
*/
protected void addCustomTypeKeyIfNecessary(TypeInformation<?> type, Object value, DBObject dbObject) {
if (type == null) {
if (type == null || type.getActualType() == null) {
return;
}

View File

@@ -712,6 +712,26 @@ public class MappingMongoConverterUnitTests {
assertThat(wrapper.listOfMaps.get(0).get("Foo"), is(Locale.ENGLISH));
}
/**
* @see DATADOC-259
*/
@Test
public void writesPlainMapOfCollectionsCorrectly() {
Map<String,List<Locale>> map = Collections.singletonMap("Foo", Arrays.asList(Locale.US));
DBObject result = new BasicDBObject();
converter.write(map, result);
assertThat(result.containsField("Foo"), is(true));
assertThat(result.get("Foo"), is(notNullValue()));
assertThat(result.get("Foo"), is(BasicDBList.class));
BasicDBList list = (BasicDBList) result.get("Foo");
assertThat(list.size(), is(1));
assertThat(list.get(0), is((Object) Locale.US.toString()));
}
class GenericType<T> {
T content;
}