Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-9692

Support conversion from Enum Interface

    XMLWordPrintable

    Details

    • Last commented by a User:
      true

      Description

      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.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              pwebb Phil Webb
              Reporter:
              wilhelm.kleu@gmail.com Wilhelm Kleu
              Last updater:
              Spring Issues Spring Issues
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Days since last comment:
                3 years, 24 weeks, 5 days ago