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:
committed by
Mark Paluch
parent
1bb4324b2e
commit
f86447bd04
@@ -954,7 +954,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)) {
|
||||
|
||||
@@ -21,6 +21,8 @@ import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.data.mongodb.core.DocumentTestUtils.*;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.net.URL;
|
||||
@@ -1803,6 +1805,22 @@ public class MappingMongoConverterUnitTests {
|
||||
converter.read(TypeWithMapOfLongValues.class, source);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1831
|
||||
public void shouldConvertArrayInConstructorCorrectly() {
|
||||
|
||||
org.bson.Document source = new org.bson.Document("array", Collections.emptyList());
|
||||
|
||||
assertThat(converter.read(WithArrayInConstructor.class, source).array, is(emptyArray()));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1831
|
||||
public void shouldConvertNullForArrayInConstructorCorrectly() {
|
||||
|
||||
org.bson.Document source = new org.bson.Document();
|
||||
|
||||
assertThat(converter.read(WithArrayInConstructor.class, source).array, is(nullValue()));
|
||||
}
|
||||
|
||||
static class GenericType<T> {
|
||||
T content;
|
||||
}
|
||||
@@ -2157,4 +2175,11 @@ public class MappingMongoConverterUnitTests {
|
||||
static class TypeWithMapOfLongValues {
|
||||
Map<String, Long> map;
|
||||
}
|
||||
|
||||
@RequiredArgsConstructor
|
||||
static class WithArrayInConstructor {
|
||||
|
||||
final String[] array;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user