Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested Object . Create target object only if source element is not Null. #1303

Closed
msundari opened this issue Sep 28, 2017 · 1 comment
Closed

Comments

@msundari
Copy link

msundari commented Sep 28, 2017

I want to map nested object. Customer.address.houseNumber to userDTO.homeDTO.addressDTO.houseNo.

Expection: If and only if Customer.address.houseNumber is not null then create homeDTO object under userDTO. Otherwise do not create any target objects.
Please advise how I should achieve this

Here is mapping that I am using.

@Mapper( nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS )
public abstract class Customer2UserMapper {
    Customer2UserMapper MAPPER = Mappers.getMapper(Customer2UserMapper.class);
    @Mapping( source="address.houseNumber", target="homeDTO.addressDTO.houseNo")
    abstract void mapKeyFinFigOverVw(Customer customer, @MappingTarget  UserDTO userDTO);

}

The generated code is below

@Generated(
    value = "org.mapstruct.ap.MappingProcessor"
)
public class Customer2UserMapperImpl extends Customer2UserMapper {

    @Override
    void mapKeyFinFigOverVw(Customer customer, UserDTO userDTO) {
        if ( customer == null ) {
            return;
        }

        if ( customer.getAddress() != null ) {
            if ( userDTO.getHomeDTO() == null ) {
                userDTO.setHomeDTO( new HomeDTO() );
            }
            addressToHomeDTO( customer.getAddress(), userDTO.getHomeDTO() );
        }
        else {
            userDTO.setHomeDTO( null );  _//should not setting to NULL. HOMEDTO contains other elements that will removed._
        }
    }

    protected void addressToAddressDTO(Address address, AddressDTO mappingTarget) {
        if ( address == null ) {  //this check is done twice . _If there is multiple hierarchical level  then this check is e  done multiple times_
            return;
        }

        if ( address.getHouseNumber() != null ) {
            mappingTarget.setHouseNo( address.getHouseNumber() );
        }
    }

    protected void addressToHomeDTO(Address address, HomeDTO mappingTarget) {
        if ( address == null ) { //this check is done twice
            return;
        }

        if ( mappingTarget.getAddressDTO() == null ) {
            mappingTarget.setAddressDTO( new AddressDTO() );
        }
        addressToAddressDTO( address, mappingTarget.getAddressDTO() );
    }
}

I would like to be generated code to be similar to

expected code.txt

@filiphr
Copy link
Member

filiphr commented Oct 3, 2017

This is really similar to #879.

Additionally, nested target properties have been overhauled in the 1.2.0 version and the generation is created differently. Please try the latest from 1.2.0 and let us know what you would expect there.

I am closing this one in favour of #879

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants