@InheritInverseConfiguration doesn't apply ignore mapping
Example:
Student.java
@Getter
@Setter
public class Student {
private String name;
private String roll;
private int age;
}
StudentDto.java
@Getter
@Setter
public class StudentDto {
private String name;
private String roll;
private int age;
}
My Mapper interface is:
@Mapper()
public interface Mapper {
@Mapping(target = "age", ignore = true)
StudentDto studentToStudentDto(Student source);
@InheritInverseConfiguration
Student studentDtoToStudent(StudentDto source);
}
The expected behavior should be ignoring setAge() method from both implementation. but it's not working. This is what I get:
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2019-12-08T20:18:06+0600",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 11.0.5 (Amazon.com Inc.)"
)
public class MapperImpl implements Mapper {
@Override
public StudentDto studentToStudentDto(Student source) {
if ( source == null ) {
return null;
}
StudentDto studentDto = new StudentDto();
studentDto.setName( source.getName() );
studentDto.setRoll( source.getRoll() );
return studentDto;
}
@Override
public Student studentDtoToStudent(StudentDto source) {
if ( source == null ) {
return null;
}
Student student = new Student();
student.setName( source.getName() );
student.setRoll( source.getRoll() );
student.setAge( source.getAge() );
return student;
}
}
As you can see in the studentDtoToStudent() method age property is not being ignored.
Please, specify source as well.. remember: source becomes target when inversion takes place
Thanks!
After specifying source parameter it's now working as expected.
remember: source becomes target when inversion takes place
I think you should add an example, so it becomes easy to understand and not miss this important detail. Maybe you can modify the @InheritInverseConfiguration example a bit like this:
@Mapper
public interface CarMapper {
@Mapping(source = "numberOfSeats", target = "seatCount")
@Mapping(source = "engineClass", target = "enginePower", ignore=true)
CarDto carToDto(Car car);
@InheritInverseConfiguration
@Mapping(target = "numberOfSeats", ignore = true)
// @Mapping(source = "enginePower", target = "engineClass", ignore=true) automatically inherited
Car carDtoToCar(CarDto carDto);
}
@imtiazShakil good point.. I added a PR with your suggestion.. Thanks!
merged PR.. @filiphr thanks for reviewing
Most helpful comment
Please, specify source as well.. remember: source becomes target when inversion takes place