14

i create mapping like below. How to map flat dto object properties like (street, city, etc) to nested address in domain object. When I try to I've got an error:

[ERROR] diagnostic: Unknown property "address.postalCode" in return type. @Mapping(source = "city", target = "address.city"),

@Mapper(componentModel = "spring", uses = {})
public interface CompanyMapper {
    @Mappings({
            @Mapping(source = "id", target = "id"),
            @Mapping(source = "street", target = "address.street"),
            @Mapping(source = "city", target = "address.city"),
            @Mapping(source = "postalCode", target = "address.postalCode"),
            @Mapping(source = "province", target = "address.province"),
    })
    DomainObject map(DtoObject dto);

And classes...

public class Address {
            private String street;
            private Integer streetNumber;
            private String city;
            private String postalCode;
            private String province;
            //getters and setters
    }
public class DomainObject {
        private String id;
        private Address address;
        //getters and setters
}

public class DtoObject {
        private String id;
        private String street;
        private String city;
        private String postalCode;
        private String province;
        //getters and setters
}
Piotr Sobolewski
  • 2,024
  • 4
  • 28
  • 42

2 Answers2

10

Nesting on the target side as you are trying to use it is not supported yet. There's a feature request for this (issue #389), but we did not yet get to implementing this.

Gunnar
  • 18,095
  • 1
  • 53
  • 73
2

I could not find a way to do that in one method. Here is my solution :

@Mapper
public interface DtoObjectMapper {

    Address toAddress(DtoObject dtoObject);

    DomainObject toDomainObject(DtoObject dtoObject, Address address);

}

while using ;

@Component
public class SomeClass {

    @Autowired
    private DtoObjectMapper dtoObjectMapper;

    public DomainObject convert(DtoObject dtoObject) {
        return dtoObjectMapper.toDomainObject(dtoObject, dtoObjectMapper.toAddress(dtoObject));
    }
}
user3088282
  • 91
  • 1
  • 9