From 69474327c61271a46e6aedb8eeb58d3663d6cd10 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 | 10 +++-- .../MappingMongoConverterUnitTests.java | 38 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 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 1df55a411..35bdafb8f 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 @@ -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 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); @@ -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())); } } 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) {