In our project we have multiple modules with OpenAPI specs being a source of generated classes, representing domain model of various versions. Now with MapStruct (version 1.6.2) I'm creating mappers for conversion between the same domain classes belonging to different modules.
Here's my mapper class:
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface AccountMapper {
@Mapping(source = "account", target = "depositAccount")
com.model5.AccountWithDetailsOneOf map(com.model6.DepositAccount account);
}
Compilation of the code produces this warning
java: Unmapped target property: "email". Mapping from Collection element "AccountHolder account.contact.holders" to "AccountHolder depositAccount.contact.holders".
The problem is that in version 6 (which is my source) the culprit field is called emails
while in version 5 (which is the target) the corresponding field's name is email
.
To fix this I modify map()
method as:
@Mapping(source = "account", target = "depositAccount")
@Mapping(source = "account.contact.holders.emails", target = "depositAccount.contact.holders.email")
com.model5.AccountWithDetailsOneOf map(com.model6.DepositAccount account);
After this recompilation fails with
java: The type of parameter "account" has no property named "contact.holders.emails".
But the property is indeed there, the only problem is it is a member of the superclass (see the screenshot below). I suspect this somehow disrupts mapping, but the reason is unclear to me, for all required getters and setters are generated with Lombok and present in target classes through the whole chain of reference.
Is it a bug/limitation imposed by MapStruct or am I doing something wrong?
You need to add another mapping
@Mapping(source = "emails", target = "email")
X mapYtoX(Y y);
where
X
is type of depositAccount.contact.holders
(I guess it is AccountHolder
)Y
is type of account.contact.holders