提交 7b189d11 编写于 作者: J Juergen Hoeller

avoid ConverterNotFoundException if source object is assignable to target type

上级 bd88bbab
......@@ -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()) {
......
......@@ -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> {
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册