Skip convert to Object from TypeConverterDelegate

Update TypeConverterDelegate to bypass conversion when the target type
is Object.class. Prior to this commit converting a single element array
to an Object would result in the element being returned, rather than
the array itself.

Issue: SPR-10996
This commit is contained in:
Phillip Webb 2013-10-18 17:19:41 -07:00
parent b25e91a550
commit c9aace4da2
2 changed files with 36 additions and 0 deletions

View File

@ -187,6 +187,9 @@ class TypeConverterDelegate {
// Try to apply some standard type conversion rules if appropriate.
if (convertedValue != null) {
if (Object.class.equals(requiredType)) {
return (T) convertedValue;
}
if (requiredType.isArray()) {
// Array required -> apply appropriate conversion of elements.
if (convertedValue instanceof String && Enum.class.isAssignableFrom(requiredType.getComponentType())) {

View File

@ -63,6 +63,10 @@ import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* @author Rod Johnson
@ -1559,6 +1563,20 @@ public final class BeanWrapperTests {
assertEquals("val1", Spr10115Bean.prop1);
}
@Test
public void testArrayToObject() throws Exception {
ArrayToObject foo = new ArrayToObject();
BeanWrapperImpl bwi = new BeanWrapperImpl();
bwi.setWrappedInstance(foo);
Object[] array = new Object[] {"1","2"};
bwi.setPropertyValue("object", array );
assertThat(foo.getObject(), equalTo((Object) array));
array = new Object[] {"1"};
bwi.setPropertyValue("object", array );
assertThat(foo.getObject(), equalTo((Object) array));
}
static class Spr10115Bean {
private static String prop1;
@ -1944,4 +1962,19 @@ public final class BeanWrapperTests {
TEST_VALUE
}
static class ArrayToObject {
private Object object;
public void setObject(Object object) {
this.object = object;
}
public Object getObject() {
return object;
}
}
}