From a530629d976ff2612b99c4d054cf3853641cff64 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 30 Jul 2012 15:39:22 +0200 Subject: [PATCH] DATAMONGO-497 - Fixed reading of empty collections. Reading an empty collection always returned a HashSet assuming the returned value would be converted into the assigned properties value later on. However the method should rather return the correct type already which we do now by invoking the potential conversion. --- .../core/convert/MappingMongoConverter.java | 5 +++-- .../MappingMongoConverterUnitTests.java | 21 +++++++++++++++++++ 2 files changed, 24 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 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) {