DATAMONGO-1831 - Fix array type conversion for empty source.

We now make sure that we convert empty sources to the corresponding target type. This prevents entity instantiation from failing due to incorrect argument types when invoking the constructor.

Original pull request: #520.
This commit is contained in:
Christoph Strobl
2017-12-01 10:40:15 +01:00
committed by Mark Paluch
parent 3ddac744ce
commit 867fcf6df2
2 changed files with 27 additions and 2 deletions

View File

@@ -904,7 +904,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
: CollectionFactory.createCollection(collectionType, rawComponentType, sourceValue.size());
if (sourceValue.isEmpty()) {
return getPotentiallyConvertedSimpleRead(items, collectionType);
return getPotentiallyConvertedSimpleRead(items, targetType.getType());
}
if (!DBRef.class.equals(rawComponentType) && isCollectionOfDbRefWhereBulkFetchIsPossible(sourceValue)) {

View File

@@ -21,6 +21,8 @@ import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.springframework.data.mongodb.core.DBObjectTestUtils.*;
import lombok.RequiredArgsConstructor;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
@@ -97,7 +99,7 @@ import com.mongodb.util.JSON;
/**
* Unit tests for {@link MappingMongoConverter}.
*
*
* @author Oliver Gierke
* @author Patrik Wasik
* @author Christoph Strobl
@@ -1797,6 +1799,22 @@ public class MappingMongoConverterUnitTests {
assertThat(converter.read(ClassWithEnumProperty.class, source).enumSet, is(EnumSet.noneOf(SampleEnum.class)));
}
@Test // DATAMONGO-1831
public void shouldConvertArrayInConstructorCorrectly() {
DBObject source = new BasicDBObject("array", Collections.emptyList());
assertThat(converter.read(WithArrayInConstructor.class, source).array, is(emptyArray()));
}
@Test // DATAMONGO-1831
public void shouldConvertNullForArrayInConstructorCorrectly() {
DBObject source = new BasicDBObject();
assertThat(converter.read(WithArrayInConstructor.class, source).array, is(nullValue()));
}
static class GenericType<T> {
T content;
}
@@ -2147,4 +2165,11 @@ public class MappingMongoConverterUnitTests {
static class TypeWithPropertyInNestedField {
@Field("nested.sample") String sample;
}
@RequiredArgsConstructor
static class WithArrayInConstructor {
final String[] array;
}
}