micronaut-core icon indicating copy to clipboard operation
micronaut-core copied to clipboard

Custom TypeConverter not work when source type is Map.

Open subash89 opened this issue 3 years ago • 1 comments

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<String>,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

subash89 avatar Oct 23 '22 18:10 subash89

Can you please confirm that this is still an issue in the latest version of Micronaut?

ojebari-ma avatar May 26 '25 01:05 ojebari-ma