From dc44c3a455c0343211f328cf77c88e1472ef535c Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 7 Nov 2016 15:23:23 +0100 Subject: [PATCH] DATAMONGO-1525 - Improved creation of empty collections, esp. EnumSet. We now use more type information to create a better empty collection in the first place. The previous algorithm always used an empty HashSet plus a subsequent conversion using the raw collection type. Especially the latter caused problems for EnumSets as the conversion into one requires the presence of component type information. We now use Spring's collection factory and more available type information to create a proper collection in the first place and only rely on a subsequent conversion for arrays. --- .../mongodb/core/convert/MappingMongoConverter.java | 8 ++++---- .../core/convert/MappingMongoConverterUnitTests.java | 11 +++++++++++ 2 files changed, 15 insertions(+), 4 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 35b6a3179..ea2da3c45 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 @@ -886,10 +886,6 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App Class collectionType = targetType.getType(); - if (sourceValue.isEmpty()) { - return getPotentiallyConvertedSimpleRead(new HashSet(), collectionType); - } - TypeInformation componentType = targetType.getComponentType(); Class rawComponentType = componentType == null ? null : componentType.getType(); @@ -897,6 +893,10 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App Collection items = targetType.getType().isArray() ? new ArrayList() : CollectionFactory.createCollection(collectionType, rawComponentType, sourceValue.size()); + if (sourceValue.isEmpty()) { + return getPotentiallyConvertedSimpleRead(items, collectionType); + } + for (Object dbObjItem : sourceValue) { if (dbObjItem instanceof DBRef) { 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 f8d00bc8b..a1530194a 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 @@ -2086,6 +2086,17 @@ public class MappingMongoConverterUnitTests { assertThat(result.sample, is("value")); } + /** + * @see DATAMONGO-1525 + */ + @Test + public void readsEmptyEnumSet() { + + DBObject source = new BasicDBObject("enumSet", new BasicDBList()); + + assertThat(converter.read(ClassWithEnumProperty.class, source).enumSet, is(EnumSet.noneOf(SampleEnum.class))); + } + static class GenericType { T content; }