Issue in the mapping when sub-type in embedded twice...

438 views
Skip to first unread message

jan.v...@jdsu.com

unread,
Jun 16, 2015, 3:47:48 AM6/16/15
to selma...@googlegroups.com
Hello,

I am currently evaluating Selma to replace an old homegrown solution and I stumbled upon following issue.


Model side:
public class Person {
   
private String name;

   
private Address address; // the main address
   
private List< Address > additionalAddresses = Collections.emptyList();
   
// +setters/getters
}


public class Address {
   
private String street;
   
private String zipCode;
   
private String city;
   
private String country;
   
// +setters/getters
}




DTO side:
public class PersonDto {
   
private String name;
   
private String addressStreet;
   
private String addressZipCode;
   
private String addressCity;
   
private String addressCountry;
   
private List< AddressDto > additionalAddresses = Collections.emptyList();

   
// +setters/getters
}


public class AddressDto {
   
private String street;
   
private String zipCode;
   
private String city;
   
private String country;
   
// +setters/getters
}


Mapper:
@Mapper(
    withCustomFields
= {
       
@Field({"address.street","addressStreet"}),
       
@Field({"address.zipCode","addressZipCode"}),
       
@Field({"address.city","addressCity"}),
       
@Field({"address.country","addressCountry"})
   
}
)
public interface PersonMapper {
   
Person asModel( PersonDto personDto );
   
PersonDto asDto( Person person);
}


Outcome:

error: setter for field city from source bean org.gradle.Address is missing in destination bean org.gradle.AddressDto !
    PersonDto asDto( Person person);
              ^
   --> Add @Mapper(withIgnoreFields="org.gradle.Address.city") / @Maps(withIgnoreFields="org.gradle.Address.city") to mapper interface / method or add missing getter or specify corresponding @Field to customize field to field mapping
error: setter for field country from source bean org.gradle.Address is missing in destination bean org.gradle.AddressDto !
   --> Add @Mapper(withIgnoreFields="org.gradle.Address.country") / @Maps(withIgnoreFields="org.gradle.Address.country") to mapper interface / method or add missing getter or specify corresponding @Field to customize field to field mapping
8 errors


However, if I remove either of the (main) address or the additional addresses, this works... The files are in the attached zip file.

Any clue?
selmabug.zip

jan.v...@jdsu.com

unread,
Jun 16, 2015, 8:39:12 AM6/16/15
to selma...@googlegroups.com
FWIW, I found a working solution... I guess I had not really understood what the custom mapping was trying to do... 

@Mapper(
    withCustomFields
= {
       
@Field({"org.gradle.Person.address.street", "org.gradle.PersonDto.addressStreet"}),
       
@Field({"org.gradle.Person.address.zipCode","org.gradle.PersonDto.addressZipCode"}),
       
@Field({"org.gradle.Person.address.city",   "org.gradle.PersonDto.addressCity"}),
       
@Field({"org.gradle.Person.address.country","org.gradle.PersonDto.addressCountry"})

Séven Le Mesle

unread,
Jun 16, 2015, 8:56:59 AM6/16/15
to selma...@googlegroups.com
Hi, 

this fix sounds good, but you don't need to specify the package. For custom property mapping Selma uses an ignore case matching and in this case it can match both the Address class and the address field. 
In this case, the error report looks bad, and I really don't know why Selma do not find the AddressDto fields. I'll work on it and trie a fix. 

Could you open an issue ?

Thanks for using Selma and working on it.

Séven

jan.v...@jdsu.com

unread,
Jun 16, 2015, 9:17:06 AM6/16/15
to selma...@googlegroups.com
Ah-ha... the case insensitive matching is what I had been missing to properly understand the issue... So since "address" matches both the class and the field name, Selma was silently assuming one of them, right?
I saw that the package name was not mandatory but since I was feeling that it was matching things beyond my understanding, I thought that I'd rather explicitly use the FQCN...

BTW, the error messages are indeed not very readable in general (besides the bug I described)... I would think that something like:
setter for field city from source bean org.gradle.Address is missing in destination bean org.gradle.AddressDto !
could benefit from a shorter phrasing such as:
Missing setter for org.gradle.AddressDto.city (from org.gradle.Address.city)
And then the hints (--> Add @Mapper(withIgnoreFields="org.gradle.Address.city") / @Maps(withIgnoreFields="org.gradle.Address.city") to mapper interface / method or add missing getter or specify corresponding @Field to customize field to field mapping) do not really bring a lot of value once you understand the principles of Selma... My 2 cents... What's your opinion?

OK, I will file a bug regarding this issue...

PS : Bravo pour cette lib' ;-)

Séven Le Mesle

unread,
Jun 16, 2015, 7:40:21 PM6/16/15
to selma...@googlegroups.com
My first idea is to give clear feedback to the developper using the compiler message, it is useful to help API discovery. 
But we clearly can improve those messages,  hints are still helpful. 

Thanks for your feedback :).
Reply all
Reply to author
Forward
0 new messages