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:
@@ -713,10 +713,10 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
*
|
||||
* @param targetType must not be {@literal null}.
|
||||
* @param sourceValue must not be {@literal null}.
|
||||
* @return the converted {@link Collections}, will never be {@literal null}.
|
||||
* @return the converted {@link Collection} or array, will never be {@literal null}.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private Collection<?> readCollectionOrArray(TypeInformation<?> targetType, BasicDBList sourceValue, Object parent) {
|
||||
private Object readCollectionOrArray(TypeInformation<?> targetType, BasicDBList sourceValue, Object parent) {
|
||||
|
||||
Assert.notNull(targetType);
|
||||
|
||||
@@ -746,7 +746,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
}
|
||||
}
|
||||
|
||||
return items;
|
||||
return getPotentiallyConvertedSimpleRead(items, targetType.getType());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -948,7 +948,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
} else if (value instanceof DBRef) {
|
||||
return (T) (rawType.equals(DBRef.class) ? value : read(type, ((DBRef) value).fetch(), parent));
|
||||
} else if (value instanceof BasicDBList) {
|
||||
return (T) getPotentiallyConvertedSimpleRead(readCollectionOrArray(type, (BasicDBList) value, parent), rawType);
|
||||
return (T) readCollectionOrArray(type, (BasicDBList) value, parent);
|
||||
} else if (value instanceof DBObject) {
|
||||
return (T) read(type, (DBObject) value, parent);
|
||||
} else {
|
||||
|
||||
@@ -363,7 +363,6 @@ public class MappingMongoConverterUnitTests {
|
||||
Contact contact = result.contacts.get(0);
|
||||
assertThat(contact, is(instanceOf(Person.class)));
|
||||
assertThat(((Person) contact).firstname, is("Oliver"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1232,6 +1231,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)));
|
||||
}
|
||||
|
||||
private static void assertSyntheticFieldValueOf(Object target, Object expected) {
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
@@ -1311,6 +1331,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