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 f6452dde9..3dcfb9dfe 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 @@ -720,11 +720,12 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App Assert.notNull(targetType); + Class collectionType = targetType.getType(); + if (sourceValue.isEmpty()) { - return new HashSet(); + return getPotentiallyConvertedSimpleRead(new HashSet(), collectionType); } - Class collectionType = targetType.getType(); collectionType = Collection.class.isAssignableFrom(collectionType) ? collectionType : List.class; Collection items = targetType.getType().isArray() ? new ArrayList() : CollectionFactory 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 e2acf9a6a..18d231a6e 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 @@ -1252,6 +1252,18 @@ public class MappingMongoConverterUnitTests { assertThat(values, is(arrayWithSize(2))); } + /** + * @see DATAMONGO-497 + */ + @Test + public void readsEmptyCollectionIntoConstructorCorrectly() { + + DBObject source = new BasicDBObject("attributes", new BasicDBList()); + + TypWithCollectionConstructor result = converter.read(TypWithCollectionConstructor.class, source); + assertThat(result.attributes, is(notNullValue())); + } + private static void assertSyntheticFieldValueOf(Object target, Object expected) { for (int i = 0; i < 10; i++) { @@ -1440,6 +1452,15 @@ public class MappingMongoConverterUnitTests { Long innerId; } + static class TypWithCollectionConstructor { + + List attributes; + + public TypWithCollectionConstructor(List attributes) { + this.attributes = attributes; + } + } + private class LocalDateToDateConverter implements Converter { public Date convert(LocalDate source) {