Search code examples
javadozer

Why is dozer passing in a null source object to my Configurable Custom Converter?


I'm using Dozer version 5.4.0.

I have one class with a Map in it, and another class with a List. I'm trying to write a custom converter that will take the values of the map and put it in the List. But the problem is, the converter always gets passed a null source object for the Map, even if the parent source has a populated Map. I can't figure out why this is happening, I think the converter should be passed a populated Map object.

Here is some source code that compiles and shows the problem in action:

package com.sandbox;

import org.dozer.DozerBeanMapper;
import org.dozer.loader.api.BeanMappingBuilder;
import org.dozer.loader.api.FieldsMappingOptions;

public class Sandbox {

    public static void main(String[] args) {
        DozerBeanMapper mapper = new DozerBeanMapper();
        mapper.addMapping(new MappingConfig());

        ClassWithMap parentSource = new ClassWithMap();
        ClassWithList parentDestination = mapper.map(parentSource, ClassWithList.class);

        int sourceMapSize = parentSource.getMyField().size();
        assert sourceMapSize == 1;
        assert parentDestination.getMyField().size() == 1;    //this assertion fails!
    }

    private static class MappingConfig extends BeanMappingBuilder {

        @Override
        protected void configure() {
            mapping(ClassWithMap.class, ClassWithList.class)
                    .fields("myField", "myField",
                            FieldsMappingOptions.customConverter(MapToListConverter.class, "com.sandbox.MyMapValue"));
        }
    }


}

As you can see, that second assertion fails. Here are the other classes I'm using.

MapToListConverter.java:

package com.sandbox;

import org.dozer.DozerConverter;
import org.dozer.Mapper;
import org.dozer.MapperAware;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MapToListConverter extends DozerConverter<Map, List> implements MapperAware {

    private Mapper mapper;

    public MapToListConverter() {
        super(Map.class, List.class);
    }

    @Override
    public List convertTo(Map source, List destination) {   //source is always null, why?!
        List convertedList = new ArrayList();
        if (source != null) {
            for (Object object : source.values()) {
                Object mappedItem = mapper.map(object, getDestinationClass());
                convertedList.add(mappedItem);
            }
        }
        return convertedList;
    }

    private Class<?> getDestinationClass() {
        try {
            return Class.forName(getParameter());
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException(e);
        }
    }

    @Override
    public Map convertFrom(List source, Map destination) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void setMapper(Mapper mapper) {
        this.mapper = mapper;
    }
}

ClassWithMap.java:

package com.sandbox;

import java.util.HashMap;
import java.util.Map;

public class ClassWithMap {

    private Map<String, MyMapValue> myField;

    public Map<String, MyMapValue> getMyField() {   //this method gets called by dozer, I've tested that with a break point
        if (myField == null) {
            myField = new HashMap<String, MyMapValue>();
            myField.put("1", new MyMapValue());
        }
        return myField; //myField has an entry in it when called by dozer
    }

    public void setMyField(Map<String, MyMapValue> myField) {
        this.myField = myField;
    }

}

ClassWithList.java:

package com.sandbox;

import java.util.List;

public class ClassWithList {

    private List<MyMapValue> myField;

    public List<MyMapValue> getMyField() {
        return myField;
    }

    public void setMyField(List<MyMapValue> myField) {
        this.myField = myField;
    }
}

MyMapValue.java

package com.sandbox;

public class MyMapValue {

}

The problem seems to be in the MapFieldMap.getSrcFieldValue method of dozer. These comments are added by me:

  @Override
  public Object getSrcFieldValue(Object srcObj) {
    DozerPropertyDescriptor propDescriptor;
    Object targetObject = srcObj;

    if (getSrcFieldName().equals(DozerConstants.SELF_KEYWORD)) {
      propDescriptor = super.getSrcPropertyDescriptor(srcObj.getClass());
    } else {
      Class<?> actualType = determineActualPropertyType(getSrcFieldName(), isSrcFieldIndexed(), getSrcFieldIndex(), srcObj, false);
      if ((getSrcFieldMapGetMethod() != null)
          || (this.getMapId() == null && MappingUtils.isSupportedMap(actualType) && getSrcHintContainer() == null)) {
        // Need to dig out actual map object by using getter on the field. Use actual map object to get the field value
        targetObject = super.getSrcFieldValue(srcObj);

        String setMethod = MappingUtils.isSupportedMap(actualType) ? "put" : getSrcFieldMapSetMethod();
        String getMethod = MappingUtils.isSupportedMap(actualType) ? "get" : getSrcFieldMapGetMethod();
        String key = getSrcFieldKey() != null ? getSrcFieldKey() : getDestFieldName();

        propDescriptor = new MapPropertyDescriptor(actualType, getSrcFieldName(), isSrcFieldIndexed(), getDestFieldIndex(),
                setMethod, getMethod, key, getSrcDeepIndexHintContainer(), getDestDeepIndexHintContainer());
      } else {
        propDescriptor = super.getSrcPropertyDescriptor(srcObj.getClass());
      }
    }

    Object result = null;
    if (targetObject != null) {
      result = propDescriptor.getPropertyValue(targetObject); //targetObject is my source map, but the result == null
    }

    return result;

  }

Solution

  • I figured out how to fix this. Still not sure if it's a bug or not, but I think it is. The solution is to change my configuration to say this:

            mapping(ClassWithMap.class, ClassWithList.class, TypeMappingOptions.oneWay())
                    .fields("myFields", "myFields"
                            , FieldsMappingOptions.customConverter(MapToListConverter.class, "com.sandbox.MyMapValue")
                    );
    

    The fix is in the TypeMappingOptions.oneWay(). When it's bidirectional, the dozer MappingsParser tries to use a MapFieldMap which causes my problem:

        // iterate through the fields and see wether or not they should be mapped
        // one way class mappings we do not need to add any fields
        if (!MappingDirection.ONE_WAY.equals(classMap.getType())) {
          for (FieldMap fieldMap : fms.toArray(new FieldMap[]{})) {
            fieldMap.validate();
    
            // If we are dealing with a Map data type, transform the field map into a MapFieldMap type
            // only apply transformation if it is map to non-map mapping.
            if (!(fieldMap instanceof ExcludeFieldMap)) {
              if ((isSupportedMap(classMap.getDestClassToMap())
                      && !isSupportedMap(classMap.getSrcClassToMap()))
                  || (isSupportedMap(classMap.getSrcClassToMap())
                      && !isSupportedMap(classMap.getDestClassToMap()))
                  || (isSupportedMap(fieldMap.getDestFieldType(classMap.getDestClassToMap()))
                      && !isSupportedMap(fieldMap.getSrcFieldType(classMap.getSrcClassToMap())))
                  || (isSupportedMap(fieldMap.getSrcFieldType(classMap.getSrcClassToMap())))
                      && !isSupportedMap(fieldMap.getDestFieldType(classMap.getDestClassToMap()))) {
                FieldMap fm = new MapFieldMap(fieldMap);
                classMap.removeFieldMapping(fieldMap);
                classMap.addFieldMapping(fm);
                fieldMap = fm;
              }
            }