15

I am trying to map nested properties using mapstruct 1.2.0.CR2. (Example map customer.address.houseNumber to userDTO.homeDTO.addressDTO.houseNo ).

Expectation : I do not want to set the addressDTO to null when customer.address is null. Since addressDTO contains "countyname" and other properties which are already set from other different sources.

Please advice if there is property/setting that I could set so that the target it not set to null when source is null.

@Mapper( nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS )
public interface CustomerUserMapperNullCheck {

    @Mapping(source="address", target="homeDTO.addressDTO" )
    void mapCustomer(Customer customer, @MappingTarget  UserDTO userDTO)  ;

    @Mapping(source="houseNumber", target="houseNo" )
    void mapCustomerHouse(Address address, @MappingTarget  AddressDTO addrDTO)  ;

}

I initially tried in single mapping like below

@Mapping(target="homeDTO.addressDTO.houseNo", source="address.houseNumber")
 abstract void mapCustomerHouse(Customer customer, @MappingTarget  UserDTO userDTO)  ; 

Then tried splitting up the mapping, based on https://github.com/mapstruct/mapstruct/issues/649.

Both approaches does not produce the expected result/ Generated method code

 protected void customerToHomeDTO(Customer customer, HomeDTO mappingTarget) {
        if ( customer == null ) {
            return;
        }

        if ( customer.getAddress() != null ) {
            if ( mappingTarget.getAddressDTO() == null ) {
                mappingTarget.setAddressDTO( new AddressDTO() );
            }
            mapCustomerHouse( customer.getAddress(), mappingTarget.getAddressDTO() );
        }
        **else {
            mappingTarget.setAddressDTO( null );   // I dont want to else where addressDTO is set to null.
        }**
    }

The complete generated code is here
https://github.com/mapstruct/mapstruct/issues/1306

Thanks

ikhvjs
  • 5,316
  • 2
  • 13
  • 36
Malathi Damodaran
  • 229
  • 1
  • 3
  • 8

4 Answers4

31
@Mapper( nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE )
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
Alex
  • 454
  • 5
  • 5
3

For me it worked like this:

@Mapper(nullValueMappingStrategy =  NullValueMappingStrategy.RETURN_DEFAULT)
public interface MyMapper {
...
}
andreyro
  • 935
  • 12
  • 18
3

For me only this one works, as explained here: https://github.com/mapstruct/mapstruct/issues/649

@Mapper( nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS )
tak3shi
  • 2,305
  • 1
  • 20
  • 33
1

This worked for me:

@BeanMapping(nullValuePropertyMappingStrategy =  NullValuePropertyMappingStrategy.IGNORE)
void updateProduct(@MappingTarget Product entity, ProductRequestDto dto);

@Mapping annotation requires "target" option in current (1.5.5.Final) MapStruct release.

Thanks for @tak3shi link on github discussion!

PureTruth
  • 11
  • 1
  • Adding MappingTarget adnotation as well as BeanMapping(nullValuePropertyMappingStrategy did the trick for me. It seems that without MappingTarget mapper implementation generates but ignoring nullValuePropertyMappingStrategy – Artur Łysik Aug 23 '23 at 11:22