Description
Wilhelm Kleu opened SPR-9692 and commented
I noticed the following issue while upgrading my Spring version from version 3.0.6 to version 3.1.2.
getMatchingConverterForTarget
in GenericConversionService
will return the default converter StringToEnumConverterFactory
even if I implement my own ConverterFactory<String, MyInterface>
and the enum implements MyInterface
.
In GenericConversionService.getMatchingConverterForTarget(...)
the class queue first tries to match a converter of the base class and if none is found it will try to match the super class (Enum.class
) and will find StringToEnumConverterFactory
. It does not try to match converters to the interfaces.
Possible solution:
Don't add superclass to classQueue if an enum:
GenericConversionService:433
:
if (superClass != null && superClass != Object.class && superClass != Enum.class) {
And then try to match a converter for Enum.class
if no other converter is found:
GenericConversionService:447
:
if (targetObjectType.isEnum()) {
GenericConverter converter = matchConverter(converters.get(Enum.class), sourceType, targetType);
if (converter != null) {
return converter;
}
}
return matchConverter(converters.get(Object.class), sourceType, targetType);
Test case attached.
Affects: 3.1.2
Attachments:
Issue Links:
- Refactor GenericConversionService [SPR-9927] #14560 Refactor GenericConversionService ("depends on")
- String to Enum Interface conversion not working [SPR-12050] #16666 String to Enum Interface conversion not working