Browse Source

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.
pull/663/head
Oliver Gierke 9 years ago
parent
commit
97f8374be0
  1. 8
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java
  2. 11
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

8
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 @@ -886,10 +886,6 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
Class<?> collectionType = targetType.getType();
if (sourceValue.isEmpty()) {
return getPotentiallyConvertedSimpleRead(new HashSet<Object>(), collectionType);
}
TypeInformation<?> componentType = targetType.getComponentType();
Class<?> rawComponentType = componentType == null ? null : componentType.getType();
@ -897,6 +893,10 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App @@ -897,6 +893,10 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
Collection<Object> items = targetType.getType().isArray() ? new ArrayList<Object>()
: CollectionFactory.createCollection(collectionType, rawComponentType, sourceValue.size());
if (sourceValue.isEmpty()) {
return getPotentiallyConvertedSimpleRead(items, collectionType);
}
for (Object dbObjItem : sourceValue) {
if (dbObjItem instanceof DBRef) {

11
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

@ -2086,6 +2086,17 @@ public class MappingMongoConverterUnitTests { @@ -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> {
T content;
}

Loading…
Cancel
Save