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.
This commit is contained in:
@@ -720,11 +720,12 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
|
||||
Assert.notNull(targetType);
|
||||
|
||||
Class<?> collectionType = targetType.getType();
|
||||
|
||||
if (sourceValue.isEmpty()) {
|
||||
return new HashSet<Object>();
|
||||
return getPotentiallyConvertedSimpleRead(new HashSet<Object>(), collectionType);
|
||||
}
|
||||
|
||||
Class<?> collectionType = targetType.getType();
|
||||
collectionType = Collection.class.isAssignableFrom(collectionType) ? collectionType : List.class;
|
||||
|
||||
Collection<Object> items = targetType.getType().isArray() ? new ArrayList<Object>() : CollectionFactory
|
||||
|
||||
@@ -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<Attribute> attributes;
|
||||
|
||||
public TypWithCollectionConstructor(List<Attribute> attributes) {
|
||||
this.attributes = attributes;
|
||||
}
|
||||
}
|
||||
|
||||
private class LocalDateToDateConverter implements Converter<LocalDate, Date> {
|
||||
|
||||
public Date convert(LocalDate source) {
|
||||
|
||||
Reference in New Issue
Block a user