Properties that are collections of objects that need a converter

1,827 views
Skip to first unread message

bernhar...@gmail.com

unread,
Jun 6, 2017, 8:48:34 AM6/6/17
to modelmapper
Hey,

if I have this situation:

class SOURCE {
 
List<B1> prop;
}

class TARGET {
 
List<B2> props;
}



and B1 to B2 needs a converter that I wrote. Is there any way to tell ModelMapper to use that converter without having to write another converter that iterates over the list?

Bernhard

Chun Han Hsiao

unread,
Jun 6, 2017, 11:46:56 AM6/6/17
to modelmapper
Hi,

First, the destination's property "props" didn't match to the source's property "prop", so you need have an explicit mapping by defining an PropertyMap.

modelMapper.addMappings(new PropertyMap<Source, Destination>() {
    protected void configure() {
      map(source.getProp(), destination.getProps());
    }
});

And second, you can just add your converter for B1 to B2 with modelMapper.addConverter(yourConverter);

I think the mapping will be fine with above configuration.

For detail, I think you can reference the unit test at https://gist.github.com/chhsiao90/15324ed3933b51c5f9e230af177c8af2
If still have problem, please kindly let me know, thanks.

bernhar...@gmail.com於 2017年6月6日星期二 UTC+8下午8時48分34秒寫道:

bernhar...@gmail.com

unread,
Jun 7, 2017, 3:52:25 AM6/7/17
to modelmapper
Hi there,

thanks a lot! I just realised it was a different problem on my side (working with large, auto-generated (from an XSD) models), in fact on my side the situation is like this:



static class Source {
 
private NestedSource nestedSource;
}

static class NestedSource {
 
private List<B1> prop;
}

static class Destination {
 
private List<B2> props;
}


So the list ist nested one level deeper on the source, but not on the destination.

The problem is that in this case, the error message is:

Failed to instantiate instance of destination java.util.List. Ensure that java.util.List has a non-private no-argument constructor.

Which is rather misleading and made me think it was not possible to have Lists converted automatically. Maybe the error message could be changed? And include more info? If it had said "when trying to map field nestedSource of type NestedSource onto field props of type List<B2>", I would have seen at once what the problem was. 

The fix was of course to just .getProp() here:

    modelMapper.addMappings(new PropertyMap<Source, Destination>() {
     
protected void configure() {

        map
(source.getNestedSource().getProp(), destination.getProps());
     
}
   
});



Thanks a lot for your support! I've updated the unit test to provoke the error:


Bernhard

Chun Han Hsiao

unread,
Jun 7, 2017, 6:34:50 AM6/7/17
to modelmapper

Hi,

Thanks for your report, I will check that if it possible to improve the error message for this case!


--
You received this message because you are subscribed to the Google Groups "modelmapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modelmapper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages