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
}

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

so is there any other solution to that issue? I think that gonna works if the expression is in the source !
@Gunnar Nesting on the Target side is still not supported? and if yes, then this "target side nesting" is supported with lombok out of the box?
it's possible using @InheritInverseConfiguration
Issue gas bene closed.It works in 1.10. However not flawless. Checkout #1011.
fortunately, that issue has also been solved in the meantime @lrkwz
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));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.