Good day to you mapstruct users !
I am trying to remove some boilerplate code in our mappers but I am unable to find a solution that looks good to me. I did my research on stackoverflow, on this group and in the code.
Let's say we have 2 classes CustomerDTO and Customer having identical attributes with one tiny difference : a prefix 'customer' for all attributes of the dto. (I cannot change that, and we can expect more to come in the same way)
Is there a way to avoid declaring the @Mapping which only differs by a prefix and instead declaring this prefix more globally so that the default mapping can kick in ?
Mapping
CustomerDTO <--> Customer
customerId id
customerName name
[...]
Current Implementation
@Mapper(...)
public interface CustomerMapper {
@Mapping(source = "customerId", target = "id")
@Mapping(source = "customerName", target = "name")
[...]
CustomerDTO record2dto(Customer customer);
}
Idea
I looked into the SPI feature but I am not 100% happy with my idea.
From my understanding I should be able to add/remove the property prefix in the method org.mapstruct.ap.spi.DefaultAccessorNamingStrategy#getPropertyName at the right time by checking the enclosedElements and find the class where the prefix could be defined. I see one issue with this approach as the prefix would be defined in the class (custom annotation would be enough, but it is not the right place to put that) or calculated after scanning the fields of the class (would be quite obscure).
Is there a standard way to achieve what I am trying?
Thanks in advance!