Is mapping beans with List of bean properties supported by model mapper?

StackOverflow https://stackoverflow.com/questions/11033780

  •  14-06-2021
  •  | 
  •  

سؤال

I am trying to map two JavaBean structures (here simplified):

package foo;
public class Container {
    private List<Item> items;
    public List<Item> getItems() { return items; }
    public void setItems(List<Item> items) { this.items = items; }
}

public class Item {
    private String message;
    public String getMessage() { return message; }
    public void setMessage(String message) { this.message = message; }
}

In addition I have equal Beans with the same properties in package bar and do the mapping between the two like

ModelMapper mapper = new ModelMapper();
bar.Container barContainer = mapper.map(fooContainer, bar.Container.class);

where the source fooContainer contains a list of items some of which have the property message set to a String and some of them have null as property value.

In the mapping result I discover that the list of foo.Item seems to be correctly mapped to a list of bar.Item also the first message properties are mapped correctly. But after the first item with a null value of the message property all message property values of hte following items are mapped to null regardless of the content of the source properties.

With debugging I found out why: Once the destination property value is null the message property path is marked as shaded in MappingEngineImpl:207 and then ignored for the subsequent items in MappingEngineImpl:142.

Is this a bug of moddelmapper or do I have to configure modelmapper in another way to enable mapping of list of bean properties?

هل كانت مفيدة؟

المحلول

The definitely looks like a bug to me. I actually encountered the very same issue with my own use of ModelMapper which is how I found this question.

Shading paths that are null seems dangerous since the same context is used to map other objects. I have taken this feature out and it seems to work as expected.

I created an issue on the ModelMapper page around this bug: http://code.google.com/p/modelmapper/issues/detail?id=19

Also, I fixed the issue and posted it to GitHub. I have made a pull request and hopefully it will be incorporated into the next version of ModelMapper: https://github.com/chrisdail/modelmapper

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top