diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java index 3e118cf0e..f539a04b3 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java @@ -650,8 +650,11 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App Assert.notNull(targetType); + Class collectionType = targetType.getType(); + collectionType = Collection.class.isAssignableFrom(collectionType) ? collectionType : List.class; + Collection items = targetType.getType().isArray() ? new ArrayList() : CollectionFactory - .createCollection(targetType.getType(), sourceValue.size()); + .createCollection(collectionType, sourceValue.size()); for (int i = 0; i < sourceValue.size(); i++) { Object dbObjItem = sourceValue.get(i); @@ -660,7 +663,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())); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java index 3b7360b15..6698a5daf 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java @@ -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 values = (Collection) read.attributes.get(0).value; + + assertThat(values.size(), is(2)); + assertThat(values, hasItems("1", "2")); + } + static class GenericType { T content; } @@ -1051,6 +1080,15 @@ public class MappingMongoConverterUnitTests { } } + static class Item { + List attributes; + } + + static class Attribute { + String key; + Object value; + } + private class LocalDateToDateConverter implements Converter { public Date convert(LocalDate source) {