Reputation: 345
I am trying to write a mapper class using Mapstruct that makes use of another, nested mapper.
When I define a merge-style mapping method with a mapping target, the generated code uses the nested mapper, but it does not assign the return value of the nested mapper to the corresponding property in the destination object. Instead, it assumes that the nested mapper just modifies the destination parameter which would violate immutability of the NestedDestination
instance.
Is there a way to force Mapstruct to assign the return value of a nested mapper?
The snippets below illustrate what I am trying to achieve.
@Mapper(uses = {NestedMapper.class})
public abstract class Mapper {
abstract Destination map(final Source source, @MappingTarget Destination original);
}
@Mapper
public abstract class NestedMapper {
abstract NestedDestination map(final NestedSource source, @MappingTarget NestedDestination original);
}
@Generated
public class MapperImpl extends Mapper {
private NestedMapper nestedMapper;
@Override
Destination map(Source source, Destination original) {
if ( source == null ) {
return original;
}
if ( source.getProperty() != null ) {
if ( original.getProperty() == null ) {
original.setProperty( new NestedDestination() );
}
// I expect the line below to be
// original.setProperty(nestedMapper.map( source.getProperty(), original.getProperty() ));
nestedMapper.map( source.getProperty(), original.getProperty() );
}
else {
original.setProperty( null );
}
return original;
}
}
Upvotes: 0
Views: 35