From ea9b402547e83c2d0ba3454251a8d86da78627fe 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 344170d9a..4ffd911c3 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 @@ -892,10 +892,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(); @@ -903,6 +899,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); + } + if (!DBRef.class.equals(rawComponentType) && isCollectionOfDbRefWhereBulkFetchIsPossible(sourceValue)) { return bulkReadAndConvertDBRefs((List) (List) (sourceValue), componentType, path, rawComponentType); } 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; }