Skip to content

Hierarchical object to flat object mappings. Can't define explicit custom mapping of nested object #2581

@linarkou

Description

@linarkou

Continuing #1406
I see the test FlatteningMapper.java , but here nested source and target have same field names and it works perfectly.

But if we modify the AccountDTO field name number -> num, we should explicitly define mappings.
And they are not used (inverse mappings are not used too).

Full example:

import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;

/**
 * @author Sjaak Derksen
 */
@Mapper
public interface FlatteningMapper {

    FlatteningMapper INSTANCE = Mappers.getMapper( FlatteningMapper.class );

    @Mapping(target = ".", source = "name")
    @Mapping(target = ".", source = "account")
    Customer flatten(CustomerDTO customer);

    @InheritInverseConfiguration
    CustomerDTO expand(Customer customer);

    @Mapping(target = "number", source = "num")
    void accountDTOToCustomer(@MappingTarget Customer customer, AccountDTO account);

    @InheritInverseConfiguration
    void customerToAccountDTO(@MappingTarget AccountDTO account, Customer customer);

    class Customer {

        private String name;
        private String id;
        private String details;
        private String number;

        // same getters,setters
    }

    class CustomerDTO {

        private NameDTO name;
        private AccountDTO account;

        // same getters,setters
    }

    class NameDTO {
        private String name;
        private String id;

        // same getters,setters
    }

    class AccountDTO {
        private String num;
        private String details;

        public String getNum() {
            return num;
        }

        public AccountDTO setNum(String num) {
            this.num = num;
            return this;
        }

        public String getDetails() {
            return details;
        }

        public AccountDTO setDetails(String details) {
            this.details = details;
            return this;
        }
    }

}

And one more notice. If we modify flatten(...) and expand(...) methods signature to void (with @MappingTarget) , then inverse mappings are used correctly , but forward mappings still are not used.

    @Mapping(target = ".", source = "name")
    @Mapping(target = ".", source = "account")
    void flatten(@MappingTarget Customer customer, CustomerDTO customerDTO);

    @InheritInverseConfiguration
    void expand(@MappingTarget CustomerDTO customerDTO, Customer customer);

    @Mapping(target = "number", source = "num")
    void accountDTOToCustomer(@MappingTarget Customer customer, AccountDTO account);

    @InheritInverseConfiguration
    void customerToAccountDTO(@MappingTarget AccountDTO account, Customer customer);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions