orika-mapper / orika

Simpler, better and faster Java bean mapping framework
http://orika-mapper.github.io/orika-docs/
Apache License 2.0
1.29k stars 269 forks source link

can not converter source list to destination list with same enum property #382

Open steelcg opened 2 years ago

steelcg commented 2 years ago

enum:

public enum YNEnum {
    Y,
    N
}

source type:

public class KeyValuePO {
    private Integer key;
    private String value;
    private YNEnum ynEnum;
...

destination type:

public class KeyValueVO {
    private Integer key;
    private String value;
    private YNEnum ynEnum;
...

converter code

   public static <S, D> List<D> getDeeplyCopiedObject(List<S> source, Class<D> destinationType) {
        Assert.notNull(source, "Source must not be null");
        Assert.notNull(destinationType, "Destination Type Type must not be null");
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        mapperFactory.classMap(source.getClass(), destinationType).byDefault().register();
        MapperFacade mapper = mapperFactory.getMapperFacade();
        return mapper.mapAsList(source, destinationType);
    }

and the invoke code

        List<KeyValuePO> keyValues = new ArrayList<>();
        keyValues.add(new KeyValuePO(1, "v1", YNEnum.Y));
        keyValues.add(new KeyValuePO(2, "V2", YNEnum.N));
        List<KeyValueVO> deeplyCopiedObject = ObjectUtil.getDeeplyCopiedObject(keyValues, KeyValueVO.class);

then we get the error:

13:40:27.036 [main] DEBUG ma.glasnost.orika.metadata.ClassMapBuilder - ClassMap created:
    ClassMapBuilderForLists.map(ArrayList<Object>, KeyValueVO)
     .field( 1(Object), key(Integer) )
     .field( 2(Object), value(String) )
     .field( 3(Object), ynEnum(YNEnum) )
Disconnected from the target VM, address: '127.0.0.1:59176', transport: 'socket'
14:01:15.316 [main] DEBUG ma.glasnost.orika.impl.generator.MapperGenerator - Generating new mapper for (ArrayList<Object>, KeyValueVO)
    Orika_KeyValueVO_ArrayList_Mapper525728805489300$0.mapAToB(ArrayList<Object>, KeyValueVO) {
     Field(1(Object), key(Integer)) : mapping multi-occurrence element of type Object to object
     Field(2(Object), value(String)) : converting using builtin:ToStringConverter<Object, Object>
<---- ERROR occurred here

ma.glasnost.orika.MappingException: ma.glasnost.orika.MappingException: While attempting the following mapping:
sourceType = java.util.ArrayList<Object>
sourceProperty = 3(Object)
destinationType = ...KeyValueVO
destinationProperty = ynEnum(YNEnum)
Error occurred: ma.glasnost.orika.MappingException: Encountered mapping of enum to object (or vise-versa); sourceType=Object, destinationType=YNEnum

    at ma.glasnost.orika.impl.generator.MapperGenerator.build(MapperGenerator.java:104)
    at ma.glasnost.orika.impl.DefaultMapperFactory.buildMapper(DefaultMapperFactory.java:1478)
    at ma.glasnost.orika.impl.DefaultMapperFactory.build(DefaultMapperFactory.java:1293)
    at ma.glasnost.orika.impl.DefaultMapperFactory.getMapperFacade(DefaultMapperFactory.java:881)