InheritInverseConfiguration annotation example

In this post under MapStruct I will explain with example the purpose of “InheritInverseConfiguration” annotation.

Lets say you have Mapper interface that has declared a method whose purpose is to map properties of “Student” to properties of “StudentDTO”. You have used “@Mapping” annotation on the method to tell MapStruct how to map property that differ in name between “Student” and “StudentDTO”.

As shown below

@Mapper
public interface StudentMapper {
    @Mapping(target = "classVal", source = "className")
    StudentDTO getDTOFromModel(Student student);
}

In the above mapper interface, we have declared a method “getDTOFromModel” which takes “Student” object as input and returns a new object of “StudentDTO”. We are also telling MapStruct using “Mapping”
annotation declaration that property “className” in “Student” should be mapped to property “classVal” in “StudentDTO”.

Now lets say you have to declare another method which has to map properties of “StudentDTO” to “Student”. The reverse of previous method. So our mapper interface will be like this

@Mapper
public interface StudentMapper {
    @Mapping(target = "classVal", source = "className")
    StudentDTO getDTOFromModel(Student student);

    @Mapping(target = "className", source = "classVal")
    Student getModelFromDTO(StudentDTO studentDTO);
}

In the above code, you see we have added “@Mapping” annotation declaration on “getModelFromDTO” method which is a reverse of “@Mapping” annotation declaration applied on “getDTOFromModel” method.

In this example there is only one “@Mapping” annotation declaration but in real world scenario there will be many.

We can end up writing lot of “@Mapping” annotation declaration to perform reverse mapping. It will be like repeating same “@Mapping” declaration again and again only difference being “source” and
“target” value reversed.

We can avoid rewriting all the “@Mapping” annotation declaration with just one annotation and that is “@InheritInverseConfiguration”.

So the modified code of “StudentMapper” mapper will be as shown below

@Mapper
public interface StudentMapper {
    @Mapping(target = "classVal", source = "className")
    StudentDTO getDTOFromModel(Student student);

    @InheritInverseConfiguration
    Student getModelFromDTO(StudentDTO studentDTO);
}

So “@InheritInverseConfiguration” mapping is telling MapStruct to take all the “@Mapping” declarations done for “getDTOFromModel” method, reverse them and apply to “getModelFromDTO” method.

In this way we can use “@InheritInverseConfiguration” annotation.

Below is the class structure of “Student” and “StudentDTO” for your reference

Student

package package20;

public class Student {
    private int id;
    private String name;
    private String className;

    //Removed getter and setter for brevity
}

StudentDTO

package package20;

public class StudentDTO {
    private int id;
    private String name;
    private String classVal;

    //Removed getter and setter for brevity
}

Leave a comment