404-not-find / orika

Automatically exported from code.google.com/p/orika
2 stars 0 forks source link

Calls toString() on objects when using a BoundMapperFacade as a Converter #154

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Create a mapper for a DTO that contains an object, to a data class that only 
contains a String. 
The goal is to only put an attribut's value of the object contained in the DTO, 
in the String of the data.

Classes example :

class PersonData {
    private Address address;

    // Other attributes

    // Getters and setters
}

class Address {
    private String city;
    private String address;

    // Getters and setters
}

class PersonDto {
    private String city; // put personData.address.city

    // Other attributes

    // Getters and setters
}

The code for this mapper would be like :

MapperFactory personMapperFactory = new DefaultMapperFactory.Builder().build();

personMapperFactory.classMap(PersonData.class, PersonDto.class)
    .field("address.city", "city")
    .byDefault()
    .register();

BoundMapperFacade<PersonData, PersonDto> personBoundMapper = 
personMapperFactory.getMapperFacade(PersonData.class, PersonDto.class);

2. Create an other class that contains a reference to PersonData, and an other 
BoundMapperFacade for this class.

Example :

class TeamData {
    private PersonData person;

    // Getter and setter
}

class TeamDto {
    private PersonDto person;

    // Getter and setter
}

3. In order to use the previously created BoundMapperFacade<PersonData, 
PersonDto>, create a Converter<PersonData, PersonDto> that uses the mapper :

Converter<Object, Object> personConverter = new 
BidirectionalConverter<PersonData, PersonDto>() {

    public PersonData convertFrom(PersonDto dto, Type<PersonData> dataType) {
        return personBoundMapper.mapReverse(dto);
    }

    public PersonDto convertTo(PersonData data, Type<PersonDto> dtoType) {
        return personBoundMapper.map(data);
    }

}

4. Add the converter to the BoundMapperFacade for TeamData to TeamDto :

MapperFactory teamMapperFactory = new DefaultMapperFactory.Builder().build();

teamMapperFactory.classMap(TeamData.class, TeamDto.class)
    .byDefault()
    .register();

teamMapperFactory.getConverterFactory().registerConverter(personConverter);

BoundMapperFacade<TeamData, TeamDto> teamBoundMapper = 
teamMapperFactory.getMapperFacade(TeamData.class, TeamDto.class);

What is the expected output? What do you see instead?

When using the teamBoundMapper, we are supposed to get a TeamDto, with a 
PersonDto containing the city of the Person. Instead of that, I get in 
personDto.city the value of address.toString().

So, the personBoundMapper is not correctly called.

What version of the product are you using? On what operating system?

I am using the last version of Orika, on Windows 7.

I know I could create a "normal" MapperFacade, but the architecture of our 
project pushes us to create BoundMapperFacade instead, as well as for 
performance reasons.

Is there an other way to "combine" mappers than creating converters ?

Thank you for your answer !

Original issue reported on code.google.com by zarku...@gmail.com on 31 Mar 2014 at 9:25