SPR-8718 Prevent Converter<?,?> from converting target sub-type.

master
Rossen Stoyanchev 13 years ago
parent dfda4c32d5
commit 00a726b098
  1. 12
      org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java
  2. 1
      org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

@ -500,13 +500,17 @@ public class GenericConversionService implements ConfigurableConversionService {
return Collections.singleton(this.typeInfo); return Collections.singleton(this.typeInfo);
} }
public boolean matchesTargetType(TypeDescriptor targetType) {
return this.typeInfo.getTargetType().equals(targetType.getObjectType());
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) { if (source == null) {
return convertNullSource(sourceType, targetType); return convertNullSource(sourceType, targetType);
} }
return this.converter.convert(source); return this.converter.convert(source);
} }
public String toString() { public String toString() {
return this.typeInfo.getSourceType().getName() + " -> " + this.typeInfo.getTargetType().getName() + return this.typeInfo.getSourceType().getName() + " -> " + this.typeInfo.getTargetType().getName() +
" : " + this.converter.toString(); " : " + this.converter.toString();
@ -571,6 +575,12 @@ public class GenericConversionService implements ConfigurableConversionService {
} }
} }
} }
if (this.defaultConverter instanceof ConverterAdapter) {
ConverterAdapter adapter = (ConverterAdapter) this.defaultConverter;
if (!adapter.matchesTargetType(targetType)) {
return null;
}
}
return this.defaultConverter; return this.defaultConverter;
} }

@ -210,7 +210,6 @@ public class GenericConversionServiceTests {
// SPR-8718 // SPR-8718
@Test(expected=ConverterNotFoundException.class) @Test(expected=ConverterNotFoundException.class)
@Ignore("TODO")
public void convertSuperTarget() { public void convertSuperTarget() {
conversionService.addConverter(new ColorConverter()); conversionService.addConverter(new ColorConverter());
conversionService.convert("#000000", SystemColor.class); conversionService.convert("#000000", SystemColor.class);

Loading…
Cancel
Save