I want to map nested java objects. Customer.address.houseNumber
to userDTO.homeDTO.addressDTO.houseNo
.
Expectation: If and only if Customer.address.houseNumber
is not null, then create homeDTO
object under userDTO
. Otherwise, do not create any target objects.
Problem: I have used "NullValueCheckStrategy.ALWAYS"
in the mapper, but mapstruct is checking if address
is not null then it creates homeDTO
. Inside the address
, the houseNumber
is null
. I would like the null check till the houseNumber
(leaf/last level object) and then create the target objects.
How can I achieve this?
Here is mapping that I am using.
@Mapper( nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS )
public interface Customer2UserMapper {
@Mapping(source="address.houseNumber", target="homeDTO.addressDTO.houseNo" )
void mapCustomerHouse(Customer customer, @MappingTarget UserDTO userDTO) ;
}
The generated code is in https://github.com/mapstruct/mapstruct/issues/1303
customer.getAddress()=.getHouseNumber()
– Irmine