orika-mapper/orika

FieldAToB not working

nautilor opened this issue · 2 comments

I have two simple class

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Person {
    String id = UUID.randomUUID().toString();
    String username;
    String firstName;
    String lastName;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PersonDto {
    String id;
    String username;
    String firstName;
    String lastName;
}

Now when I do something like

PersonDto personDto = PersonDto.builder()
                .firstName("firstName")
                .lastName("lastName")
                .username("username")
                .id("CUSTOM_ID").build();

factory.classMap(Person.class, PersonDto.class)
                .fieldAToB("id", "id").byDefault().register();

MapperFacade mapper = factory.getMapperFacade();
Person person = mapper.map(personDto, Person.class);

I was not expecting to see the id mapped to my class Person but it does appear to be there

When I use fieldBToA instead it does not get mapped when I move from Person.class to PersonDto.class

Update:

if create a class like

@Builder //removed from Person
@NoArgsConstructor
@AllArgsConstructor
@Data
@EqualsAndHashCode(callSuper = true)
public class Worker extends Person {
    String workerId;
}

and then doing

Worker worker = mapper.map(personDto, Worker.class);

the id is now different on the Worker class but the Person still uses the one from PersonDto

@nautilor
I had the same problem before. The specified constructor needs to be displayed.

PersonDto personDto = PersonDto.builder()
                .constructorA()
                .constructorB()
                .firstName("firstName")
                .lastName("lastName")
                .username("username")
                .id("CUSTOM_ID").build();