spr-7728: empty collection conversion can return value not assignable to targetType
This commit is contained in:
parent
a7293d2961
commit
b040606cfa
|
|
@ -62,9 +62,6 @@ final class CollectionToCollectionConverter implements ConditionalGenericConvert
|
|||
return null;
|
||||
}
|
||||
Collection<?> sourceCollection = (Collection<?>) source;
|
||||
if (sourceCollection.isEmpty()) {
|
||||
return sourceCollection;
|
||||
}
|
||||
Collection target = CollectionFactory.createCollection(targetType.getType(), sourceCollection.size());
|
||||
for (Object sourceElement : sourceCollection) {
|
||||
Object targetElement = this.conversionService.convert(sourceElement, sourceType.getElementTypeDescriptor().applyIndexedObject(sourceElement), targetType.getElementTypeDescriptor());
|
||||
|
|
|
|||
|
|
@ -67,9 +67,6 @@ final class MapToMapConverter implements ConditionalGenericConverter {
|
|||
return null;
|
||||
}
|
||||
Map<?, ?> sourceMap = (Map<?, ?>) source;
|
||||
if (sourceMap.isEmpty()) {
|
||||
return sourceMap;
|
||||
}
|
||||
Map targetMap = CollectionFactory.createMap(targetType.getType(), sourceMap.size());
|
||||
for (Object entry : sourceMap.entrySet()) {
|
||||
Map.Entry sourceMapEntry = (Map.Entry) entry;
|
||||
|
|
|
|||
|
|
@ -399,6 +399,19 @@ public class GenericConversionServiceTests {
|
|||
|
||||
public List<Integer> emptyListTarget;
|
||||
|
||||
@Test
|
||||
public void emptyListToListDifferentTargetType() throws Exception {
|
||||
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
|
||||
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||
List<String> list = new ArrayList<String>();
|
||||
TypeDescriptor sourceType = TypeDescriptor.forObject(list);
|
||||
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyListDifferentTarget"));
|
||||
assertTrue(conversionService.canConvert(sourceType, targetType));
|
||||
assertEquals(LinkedList.class, conversionService.convert(list, sourceType, targetType).getClass());
|
||||
}
|
||||
|
||||
public LinkedList<Integer> emptyListDifferentTarget;
|
||||
|
||||
@Test
|
||||
public void emptyListToArray() throws Exception {
|
||||
conversionService.addConverter(new CollectionToArrayConverter(conversionService));
|
||||
|
|
@ -434,6 +447,19 @@ public class GenericConversionServiceTests {
|
|||
|
||||
public Map<String, String> emptyMapTarget;
|
||||
|
||||
@Test
|
||||
public void emptyMapToMapDifferentTargetType() throws Exception {
|
||||
conversionService.addConverter(new MapToMapConverter(conversionService));
|
||||
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
TypeDescriptor sourceType = TypeDescriptor.forObject(map);
|
||||
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapDifferentTarget"));
|
||||
assertTrue(conversionService.canConvert(sourceType, targetType));
|
||||
assertEquals(map, conversionService.convert(map, sourceType, targetType));
|
||||
}
|
||||
|
||||
public Map<String, String> emptyMapDifferentTarget;
|
||||
|
||||
private interface MyBaseInterface {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,52 @@
|
|||
package org.springframework.core.convert.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
public class Spr7728Tests
|
||||
{
|
||||
private CollectionToCollectionConverter theConverter;
|
||||
private Vector<String> theSrcVector;
|
||||
private TypeDescriptor theSrcType;
|
||||
private TypeDescriptor theTargetType;
|
||||
|
||||
@Before
|
||||
public void setup()
|
||||
{
|
||||
theSrcVector = new Vector<String>();
|
||||
theSrcType = TypeDescriptor.forObject(theSrcVector);
|
||||
theTargetType = TypeDescriptor.forObject(new ArrayList());
|
||||
theConverter = new CollectionToCollectionConverter(new GenericConversionService());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertEmptyVector_shouldReturnEmptyArrayList()
|
||||
throws Exception
|
||||
{
|
||||
theSrcVector.add("Element");
|
||||
|
||||
testCollectionConversionToArrayList(theSrcVector);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertNonEmptyVector_shouldReturnNonEmptyArrayList()
|
||||
throws Exception
|
||||
{
|
||||
testCollectionConversionToArrayList(theSrcVector);
|
||||
}
|
||||
|
||||
private void testCollectionConversionToArrayList(Collection<String> aSource)
|
||||
{
|
||||
Object myConverted = theConverter.convert(aSource, theSrcType, theTargetType);
|
||||
|
||||
Assert.assertTrue(myConverted instanceof ArrayList<?>);
|
||||
Assert.assertEquals(aSource.size(), ((ArrayList<?>) myConverted).size());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue