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:
Oliver Gierke
2012-07-30 15:39:22 +02:00
parent ba0232b187
commit a530629d97
2 changed files with 24 additions and 2 deletions

View File

@@ -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

View File

@@ -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) {