Source{
String one;
String two;
}
Destination{
String one;
Child child;
}
Child{
String two;
}
main() {
ModelMapper modelMapper = new ModelMapper();
// what configurations to set here?
Source source = new Source("one=1", "two=2");
Destination desiredResult = new Destination();
desiredResult.setOne("one=1");
Destination mappedResult = modelmapper.map(source, Destination.class )
assertEquals(mappedResult, desiredResult );
}
The problem is that modelmapper will map source.two => destination.child.two
, which I do not want. I have tried methods like
1. modelMapper.getConfiguration().setPreferNestedProperties(false);
2. modelMapper.getConfiguration().setAmbiguityIgnored(true);
3. modelMapper.getConfiguration().setImplicitMappingEnabled(false);
4. modelMapper.addMappings(new PropertyMap<Source, Destination>() {
@Override
protected void configure() {
skip(destination.getChild());
skip(destination.getChild().getTwo());
}
});
None worked.
I hope to have a general solution that can stop model mapper to map any nested object's properties in destination object.
Could you perhaps be missing some preexisting configuration? Your example works just fine for me.
Anyway, such things happen mostly when you are using LOOSE
matching strategy.
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.LOOSE);
You can fix it by explicitly setting matching strategy to STANDARD
or STRICT
.
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STANDARD);
Or
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
Check the docs for additional information on matching strategies, naming conventions, default configurations, etc.