i am having troubles mapping a nested dto field properly with mapstruct. i have a couple of dtos:
@Data
public class InstrumentGet {
.....
private MediaGet image;
private List<LocaleGet> locales;
}
@Data
public class LocaleGet {
private String name;
private String description;
private String iso6391;
private boolean defaultLanguage;
}
@Data
public class MediaGet {
private String uri;
private int width;
private int height;
}
With corresponding mappers
@Mapper(componentModel = "spring")
public interface LocaleMapper {
@Mapping(source = "culture.iso6391",target = "iso6391")
@Mapping(source = "culture.defaultLanguage",target = "defaultLanguage")
LocaleGet toDtoGet(BaseLocale locale);
List<LocaleGet> toDtosGet(List<BaseLocale> locales);
}
@Mapper( componentModel = "spring")
public interface MediaMapper {
MediaGet toDtoGet(Media media);
}
@Mapper(componentModel = "spring",uses = {MediaMapper.class, LocaleMapper.class})
public interface InstrumentMapper {
InstrumentGet instrumentToInstrumentGet(Instrument instrument, List<BaseLocale> locales);
}
So far everything works perfectly fine and the generated code autowires other needed mappers to build dtos properly. Eg. of the generated instrument mapper implementation
@Component
public class InstrumentMapperImpl implements InstrumentMapper {
@Autowired
private MediaMapper mediaMapper;
@Autowired
private LocaleMapper localeMapper;
@Override
public InstrumentGet instrumentToInstrumentGet(Instrument instrument, List<BaseLocale> locales) {
if ( instrument == null && locales == null ) {
return null;
}
InstrumentGet instrumentGet = new InstrumentGet();
if ( instrument != null ) {
instrumentGet.setDefaultName( instrument.getDefaultName() );
instrumentGet.setImage( mediaMapper.toDtoGet( instrument.getImage() ) );
}
if ( locales != null ) {
instrumentGet.setLocales( localeMapper.toDtosGet( locales ) );
}
return instrumentGet;
}
}
Now i run into trouble when trying to create a mapper that has nested instrument dto inside of it. The mapper should use instrumentMapper to create the required dto properly. The dto:
@Data
public class MemberGet {
......
private MediaGet image;
private InstrumentGet instrument;
......
}
The mapper:
@Mapper(componentModel = "spring",uses = {InstrumentMapper.class, MediaMapper.class})
public interface ClientMapper {
@Mapping(source = "member.band.uid",target = "bandUid")
MemberGet toDtoMemberGet(Member member, List<BaseLocale> locales);
}
The generated code:
@Component
public class ClientMapperImpl implements ClientMapper {
@Autowired
private MediaMapper mediaMapper;
@Override
public MemberGet toDtoMemberGet(Member member, List<BaseLocale> locales) {
if ( member == null && locales == null ) {
return null;
}
MemberGet memberGet = new MemberGet();
if ( member != null ) {
memberGet.setBandUid( memberBandUid( member ) );
memberGet.setPersonDetails( member.getPersonDetails() );
memberGet.setBirthday( member.getBirthday() );
memberGet.setGender( member.getGender() );
memberGet.setImage( mediaMapper.toDtoGet( member.getImage() ) );
memberGet.setInstrument( instrumentToInstrumentGet( member.getInstrument() ) );
}
return memberGet;
}
private String memberBandUid(Member member) {
if ( member == null ) {
return null;
}
Band band = member.getBand();
if ( band == null ) {
return null;
}
String uid = band.getUid();
if ( uid == null ) {
return null;
}
return uid;
}
protected InstrumentGet instrumentToInstrumentGet(Instrument instrument) {
if ( instrument == null ) {
return null;
}
InstrumentGet instrumentGet = new InstrumentGet();
instrumentGet.setDefaultName( instrument.getDefaultName() );
instrumentGet.setImage( mediaMapper.toDtoGet( instrument.getImage() ) );
return instrumentGet;
}
}
Now the media mapper gets picked up fine and uses the required method, I am having problem with the Instrument mapper, mapstruct does not autowire and call the required method but instead creates its own method which does not suit my needs. I am obviously doing something wrong here. Could anyone give me a hint or any kind of suggestion?
Regards
There is a limitation in mapstruct when it comes to using directly from another mapper a mapper that has several source parameters. You can test it by yourself by removing one of the two parameters of the referenced mapper.
Instead you can use @BeforeMapping or @AfterMapping or decoractor to resolve your issue.
Context parameter may also work.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments