From 3320208a1d0a413684a092290e5a6f6d0d4065ec Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 1 Feb 2012 16:07:03 +0100 Subject: [PATCH] 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. --- .../core/convert/MappingMongoConverter.java | 8 +++- .../MappingMongoConverterUnitTests.java | 38 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) 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) {