简体   繁体   中英

Mapper is not using another mapper, how to use a mapper from another mapper?

I am experimenting with MapStruct where I want to have a mapper use another mapper to combine multiple objects into one. For my tests I have three domain objects, DomainObject1 , DomainObject2 and DomainObject3 . I want to convert DomainObject1 to a DTO, TransferObjectA , which has a field containing a second DTO, TransferObjectB , that is constructed using DomainObject2 and DomainObject3 .

I have two mappers, one that converts DomainObject2 and DomainObject3 to TransferObjectB which ignores a field in DomainObject2 so it isn't converted:

@Mapper
public interface ObjectBMapper {

  @Mapping(target = "field1", ignore = true)
  TransferObjectB domainObject2ToTransferObjectB(DomainObject2 domainObject2, DomainObject3 domainObject3);
}

And a second mapper that converts DomainObject1 to TransferObjectA which also accepts DomainObject2 and DomainObject3 so they can be converted using the above mapper and put on the resulting TransferObjectA :

@Mapper(uses = ObjectBMapper.class)
public interface ObjectAMapper {

  TransferObjectA domainObject1ToTransferObjectA(DomainObject1 domainObject1, DomainObject2 domainObject2, DomainObject3 domainObject3);
}

However, the ObjectBMapper doesn't seem to be used by the ObjectAMapper and the field1 is converted (this results in an error because it is an enum ). Why is the ObjectBMapper not used and how can I make sure that is will be used by ObjectAMapper ?

MapStruct currently can only use other mappers with a single source parameter. There is an open feature request mapstruct/mapstruct#2081 about this functionality.

What you could do is to use a wrapper object instead of multiple source parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM