Skip to content

Support conversion from Enum Interface [SPR-9692] #14326

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions