DATAMONGO-358 - Fixed collection reading when property type is no a collection.
If you have a property of type object and it contains a collection we didn't property read it back in as creating the collection instance failed due to an invalid call to CollectionFactory. We now default the parameter handed to that call to List in case the property type is not a Collection at all.
This commit is contained in:
@@ -658,7 +658,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
(BasicDBList) sourceValue);
|
||||
}
|
||||
|
||||
TypeInformation<?> toType = typeMapper.readType((DBObject) sourceValue);
|
||||
TypeInformation<?> toType = typeMapper.readType((DBObject) sourceValue, prop.getTypeInformation());
|
||||
|
||||
// It's a complex object, have to read it in
|
||||
if (toType != null) {
|
||||
@@ -687,8 +687,11 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
|
||||
Assert.notNull(targetType);
|
||||
|
||||
Class<?> collectionType = targetType.getType();
|
||||
collectionType = Collection.class.isAssignableFrom(collectionType) ? collectionType : List.class;
|
||||
|
||||
Collection<Object> items = targetType.getType().isArray() ? new ArrayList<Object>() : CollectionFactory
|
||||
.createCollection(targetType.getType(), sourceValue.size());
|
||||
.createCollection(collectionType, sourceValue.size());
|
||||
|
||||
for (int i = 0; i < sourceValue.size(); i++) {
|
||||
Object dbObjItem = sourceValue.get(i);
|
||||
@@ -697,7 +700,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
} else if (dbObjItem instanceof DBObject) {
|
||||
items.add(read(targetType.getComponentType(), (DBObject) dbObjItem));
|
||||
} else {
|
||||
items.add(getPotentiallyConvertedSimpleRead(dbObjItem, targetType.getComponentType().getType()));
|
||||
TypeInformation<?> componentType = targetType.getComponentType();
|
||||
items.add(getPotentiallyConvertedSimpleRead(dbObjItem, componentType == null ? null : componentType.getType()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -929,6 +929,35 @@ public class MappingMongoConverterUnitTests {
|
||||
converter.read(DefaultedConstructorArgument.class, dbObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-358
|
||||
*/
|
||||
@Test
|
||||
public void writesListForObjectPropertyCorrectly() {
|
||||
|
||||
Attribute attribute = new Attribute();
|
||||
attribute.key = "key";
|
||||
attribute.value = Arrays.asList("1", "2");
|
||||
|
||||
Item item = new Item();
|
||||
item.attributes = Arrays.asList(attribute);
|
||||
|
||||
DBObject result = new BasicDBObject();
|
||||
|
||||
converter.write(item, result);
|
||||
|
||||
Item read = converter.read(Item.class, result);
|
||||
assertThat(read.attributes.size(), is(1));
|
||||
assertThat(read.attributes.get(0).key, is(attribute.key));
|
||||
assertThat(read.attributes.get(0).value, is(Collection.class));
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Collection<String> values = (Collection<String>) read.attributes.get(0).value;
|
||||
|
||||
assertThat(values.size(), is(2));
|
||||
assertThat(values, hasItems("1", "2"));
|
||||
}
|
||||
|
||||
static class GenericType<T> {
|
||||
T content;
|
||||
}
|
||||
@@ -1051,6 +1080,15 @@ public class MappingMongoConverterUnitTests {
|
||||
}
|
||||
}
|
||||
|
||||
static class Item {
|
||||
List<Attribute> attributes;
|
||||
}
|
||||
|
||||
static class Attribute {
|
||||
String key;
|
||||
Object value;
|
||||
}
|
||||
|
||||
private class LocalDateToDateConverter implements Converter<LocalDate, Date> {
|
||||
|
||||
public Date convert(LocalDate source) {
|
||||
|
||||
Reference in New Issue
Block a user