DATAMONGO-489 - Ensure read collections get converted to appropriate target type.
When reading BasicDBLists we now make sure the resulting collection is converted into the actual target type eventually. It might be an array and thus need an additional round of massaging before being returned as value.
This commit is contained in:
@@ -728,7 +728,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
* @return the converted {@link Collections}, will never be {@literal null}.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private Collection<?> readCollectionOrArray(TypeInformation<?> targetType, BasicDBList sourceValue) {
|
||||
private Object readCollectionOrArray(TypeInformation<?> targetType, BasicDBList sourceValue) {
|
||||
|
||||
Assert.notNull(targetType);
|
||||
|
||||
@@ -750,7 +750,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
}
|
||||
}
|
||||
|
||||
return items;
|
||||
return getPotentiallyConvertedSimpleRead(items, targetType.getType());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1095,6 +1095,27 @@ public class MappingMongoConverterUnitTests {
|
||||
assertThat(result.complexId.innerId, is(4711L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-489
|
||||
*/
|
||||
@Test
|
||||
public void readsArraysAsMapValuesCorrectly() {
|
||||
|
||||
BasicDBList list = new BasicDBList();
|
||||
list.add("Foo");
|
||||
list.add("Bar");
|
||||
|
||||
DBObject map = new BasicDBObject("key", list);
|
||||
DBObject wrapper = new BasicDBObject("mapOfStrings", map);
|
||||
|
||||
ClassWithMapProperty result = converter.read(ClassWithMapProperty.class, wrapper);
|
||||
assertThat(result.mapOfStrings, is(notNullValue()));
|
||||
|
||||
String[] values = result.mapOfStrings.get("key");
|
||||
assertThat(values, is(notNullValue()));
|
||||
assertThat(values, is(arrayWithSize(2)));
|
||||
}
|
||||
|
||||
static class GenericType<T> {
|
||||
T content;
|
||||
}
|
||||
@@ -1156,6 +1177,7 @@ public class MappingMongoConverterUnitTests {
|
||||
Map<Locale, String> map;
|
||||
Map<String, List<String>> mapOfLists;
|
||||
Map<String, Object> mapOfObjects;
|
||||
Map<String, String[]> mapOfStrings;
|
||||
}
|
||||
|
||||
static class ClassWithNestedMaps {
|
||||
|
||||
Reference in New Issue
Block a user