Skip to content

Commit 7b189d1

Browse files
committed
avoid ConverterNotFoundException if source object is assignable to target type
1 parent bd88bba commit 7b189d1

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,13 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
172172
}
173173
GenericConverter converter = getConverter(sourceType, targetType);
174174
if (converter == null) {
175-
throw new ConverterNotFoundException(sourceType, targetType);
175+
if (targetType.getType().isInstance(source)) {
176+
logger.debug("No converter found - returning assignable source object as-is");
177+
return source;
178+
}
179+
else {
180+
throw new ConverterNotFoundException(sourceType, targetType);
181+
}
176182
}
177183
Object result = ConversionUtils.invokeConverter(converter, source, sourceType, targetType);
178184
if (logger.isDebugEnabled()) {

org.springframework.expression/src/test/java/org/springframework/expression/spel/standard/PropertiesConversionSpelTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.HashMap;
2222
import java.util.Map;
2323
import java.util.Properties;
24+
import java.util.UUID;
2425

2526
import org.junit.Test;
2627

@@ -61,12 +62,26 @@ public void mapWithAllStringValues() {
6162
assertEquals("123", result);
6263
}
6364

64-
@Test // questionable, but this passes with 3.0.2.RELEASE
65+
@Test
6566
public void mapWithNonStringValue() {
6667
Map<String, Object> map = new HashMap<String, Object>();
6768
map.put("x", "1");
6869
map.put("y", 2);
6970
map.put("z", "3");
71+
map.put("a", new UUID(1, 1));
72+
Expression expression = parser.parseExpression("foo(#props)");
73+
StandardEvaluationContext context = new StandardEvaluationContext();
74+
context.setVariable("props", map);
75+
String result = expression.getValue(context, new TestBean(), String.class);
76+
assertEquals("1null3", result);
77+
}
78+
79+
@Test
80+
public void customMapWithNonStringValue() {
81+
CustomMap map = new CustomMap();
82+
map.put("x", "1");
83+
map.put("y", 2);
84+
map.put("z", "3");
7085
Expression expression = parser.parseExpression("foo(#props)");
7186
StandardEvaluationContext context = new StandardEvaluationContext();
7287
context.setVariable("props", map);
@@ -83,4 +98,9 @@ public String foo(Properties props) {
8398
}
8499
}
85100

101+
102+
@SuppressWarnings("serial")
103+
private static class CustomMap extends HashMap<String, Object> {
104+
}
105+
86106
}

0 commit comments

Comments
 (0)