0

I am trying to map one object type to another using ModelMapper. I have defined the following mapping in my PropertyMap implementation:

map().getExtended().setIncludeMaskType(MaskType.fromValue(source.getExtendedPollingType().getMaskSettings().getIncludeMask().getMaskType()));

The problem is that the source type method call source.getExtendedPollingType().getMaskSettings().getIncludeMask().getMaskType() returns String while the destination type accepts a MaskType enum

Therefore I am using MaskType.fromValue() to convert the String to enum. The problem is that value of fails with Caused by: java.lang.IllegalArgumentException since this is source method call basically returns null during configuration.

So how should I handle this use case?

mdzh
  • 1,030
  • 2
  • 17
  • 34

1 Answers1

2

OK seems that the correct way to do this is with using a Converter:

using((MappingContext<String, MaskType> context) -> {
    return MaskType.fromValue(context.getSource());
}).
map(source.getExtendedPollingType().getMaskSettings().getIncludeMask().getMaskType()).getExtended().setIncludeMaskType(null);

This is explained in more details here: https://github.com/modelmapper/modelmapper/issues/20

mdzh
  • 1,030
  • 2
  • 17
  • 34