@InheritInverseConfiguration

Re-uses @Mapping annotations from another method but inverts source and target.

@InheritInverseConfiguration

Copies @Mapping annotations from a designated source method and swaps the source and target in each annotation. This produces a reverse mapping without duplicating the annotation list.

Signature

const InheritInverseConfiguration()

No parameters.

Usage

Define a forward mapping, then declare the reverse method and annotate it with @InheritInverseConfiguration:

@Mapper()
abstract class PersonMapper {
  @Mapping(target: 'fullName', source: 'name')
  @Mapping(target: 'yearsOld', source: 'age')
  PersonDto toDto(Person person);

  @InheritInverseConfiguration()
  Person fromDto(PersonDto dto);
}

The generator inverts each @Mapping:

  • target: 'fullName', source: 'name' becomes target: 'name', source: 'fullName'
  • target: 'yearsOld', source: 'age' becomes target: 'age', source: 'yearsOld'

The resulting fromDto reads dto.fullNameperson.name and dto.yearsOldperson.age.

Requirements

  • The annotated method's parameter type must be assignment-compatible with the return type of the method it inherits from — the generator needs to align source and target roles.
  • During inversion, only @Mapping annotations that declare both an explicit source and target are loaded. Mappings that specify ignore: true alongside both source and target are preserved in the inverse. Mappings that lack either source or target (e.g., @Mapping(target: 'field', ignore: true)) are excluded from inverse loading because there is no value to swap.

Combining with Additional @Mapping

Add @Mapping alongside @InheritInverseConfiguration to further customize the inverse mapping:

@Mapper()
abstract class PersonMapper {
  @Mapping(target: 'fullName', source: 'name')
  PersonDto toDto(Person person);

  @InheritInverseConfiguration()
  @Mapping(target: 'createdAt', ignore: true)
  Person fromDto(PersonDto dto);
}

fromDto inherits the inverted name rename and additionally ignores createdAt.

See Also