Skip to content

Custom TypeConverter not work when source type is Map. #8214

Open
@subash89

Description

@subash89

Expected Behavior

Custom TypeConverters should invoked for source type Map.

Actual Behaviour

MyCustomTypeConverter not getting invoked when source type is Java Map. But if I use a List or CharSequence as source type like, MyCustomTypeConverter<List,CustomTypeModel>, then the converter getting invoked. With a Map(which is what I want, the typeconverter not even getting invoked. Hence CustomTypeModel set as null.

@ConfigurationProperties("app.main")
@Primary
@BootstrapContextCompatible
@Introspected
public class MConfiguration {

        private CustomTypeModel model;

        public CustomTypeModel getModel() {
                return model;
        }

        public void setModel(CustomTypeModel model) {
                this.model = model;
        }
}

public class CustomTypeModel {

                private String name;

                public String getName() {
                        return name;
                }
                public void setName(String name) {
                        this.name = name;
                }
}

@Singleton
public class MyCustomTypeConverter implements TypeConverter<Map<String,String> , CustomTypeModel> {

    @Override
    public Optional<CustomTypeModel> convert(Map<String,String> object, Class<CustomTypeModel> targetType, ConversionContext context) {
        if(object != null){
            CustomTypeModel customTypeModel = new CustomTypeModel();
            customTypeModel.setName(object.get("key1"));
            return Optional.of(customTypeModel);
        }
        return Optional.empty();
    }
}

And I am trying to input the map as follows in my application.yml

app:
   main:
     model:
        key1: YYYYY
        key2: UUUU

Steps To Reproduce

No response

Environment Information

JDK 11

Example Application

Version

3.7.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions