Reputation: 183
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
?
Upvotes: 3
Views: 3620
Reputation: 21403
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.
Upvotes: 1