avoid ConverterNotFoundException if source object is assignable to target type

This commit is contained in:
Juergen Hoeller 2010-06-15 09:35:39 +00:00
parent bd88bbab4a
commit 7b189d1124
2 changed files with 28 additions and 2 deletions

View File

@ -172,7 +172,13 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
GenericConverter converter = getConverter(sourceType, targetType);
if (converter == null) {
throw new ConverterNotFoundException(sourceType, targetType);
if (targetType.getType().isInstance(source)) {
logger.debug("No converter found - returning assignable source object as-is");
return source;
}
else {
throw new ConverterNotFoundException(sourceType, targetType);
}
}
Object result = ConversionUtils.invokeConverter(converter, source, sourceType, targetType);
if (logger.isDebugEnabled()) {

View File

@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import org.junit.Test;
@ -61,12 +62,26 @@ public class PropertiesConversionSpelTests {
assertEquals("123", result);
}
@Test // questionable, but this passes with 3.0.2.RELEASE
@Test
public void mapWithNonStringValue() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("x", "1");
map.put("y", 2);
map.put("z", "3");
map.put("a", new UUID(1, 1));
Expression expression = parser.parseExpression("foo(#props)");
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("props", map);
String result = expression.getValue(context, new TestBean(), String.class);
assertEquals("1null3", result);
}
@Test
public void customMapWithNonStringValue() {
CustomMap map = new CustomMap();
map.put("x", "1");
map.put("y", 2);
map.put("z", "3");
Expression expression = parser.parseExpression("foo(#props)");
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("props", map);
@ -83,4 +98,9 @@ public class PropertiesConversionSpelTests {
}
}
@SuppressWarnings("serial")
private static class CustomMap extends HashMap<String, Object> {
}
}