Using mapstruct v. 1.5.5, java v. 22.
Let’s say I have some entities that I am wanting to map to DTOs. The DTOs are java records.
My base entity looks like this (simplified):
public abstract class DateRule implements Predicate<LocalDate>
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Long id;
}
Let’s say I then have two subclasses from DateRule: AnnuaDateRule and DayOfWeekRule. I then have two java records for AnnualDateRuleDTO and DayOfWeekRuleDTO. The records are defined as follows:
public record AnnualDateRuleDTO(Long id, String name, @NotNull Month targetMonth, @NotNull DayOfWeek targetDayOfWeek, @NotNull int nthOccurrence) implements DateRuleDTO {}
public record DayOfWeekRuleDTO(Long id, String name, @NotNull DayOfWeek dayToMatch) implements DateRuleDTO {}
My mapper class is defined as follows:
@Mapper(componentModel = "spring", subclassExhaustiveStrategy = SubclassExhaustiveStrategy.RUNTIME_EXCEPTION)
public interface DateRuleDTOMapper
{
@SubclassMapping(source = AnnualDateRule.class, target = AnnualDateRuleDTO.class)
@SubclassMapping(source = DayOfWeekRule.class, target = DayOfWeekRuleDTO.class)
DateRuleDTO dateRuleToDTO(DateRule rule);
@SubclassMapping(source = AnnualDateRuleDTO.class, target = AnnualDateRule.class)
@SubclassMapping(source = DayOfWeekRuleDTO.class, target = DayOfWeekRule.class)
DateRule dateRuleFromDTO(DateRuleDTO dto);
}
According to the javadocs for the @SubclassMapping annotation, “This annotation can be combined with @Mapping annotations.” However, I don’t see any documentation on how to do so.
At compile time, I get warnings that the id field is unmapped DateRuleDTOMapper.java:18: warning: Unmapped target property: "id". Mapping from SubclassMapping for us.lawyertools.datecalculator.core.AnnualDateRule "AnnualDateRule annualDateRule" to "AnnualDateRuleDTO annualDateRuleDTO". DateRuleDTO dateRuleToDTO(DateRule rule);
I get similar warnings for the other classes.
So, even though the AnnualDateRule inherits the ‘id’ field from its parent, DateRule, mapstruct is not mapping it to the target ‘id’ property on AnnualDateRuleDTO.
At runtime, as you would expect given the warning, the mapper returns the DTO records with id being null.
My attempt to annotate the above dateRuleToDTO method with an @Mapping results in a compiler error. With the @Mapping annotation, the mapper looks as follows:
@Mapper(componentModel = "spring", subclassExhaustiveStrategy = SubclassExhaustiveStrategy.RUNTIME_EXCEPTION)
public interface DateRuleDTOMapper
{
@SubclassMapping(source = AnnualDateRule.class, target = AnnualDateRuleDTO.class)
@SubclassMapping(source = DayOfWeekRule.class, target = DayOfWeekRuleDTO.class)
@Mapping(source="id", target="id")
DateRuleDTO dateRuleToDTO(DateRule rule);
@SubclassMapping(source = AnnualDateRuleDTO.class, target = AnnualDateRule.class)
@SubclassMapping(source = DayOfWeekRuleDTO.class, target = DayOfWeekRule.class)
DateRule dateRuleFromDTO(DateRuleDTO dto);
}
The compiler error: DateRuleDTOMapper.java:18: error: No property named "id" exists in source parameter(s). Did you mean "name"? @Mapping(source="id", target="id") ^
If I change DateRule’s id field to be public, the compiler next errors out: DateRuleDTOMapper.java:18: error: Unknown property "id" in result type DateRuleDTO. Did you mean "null"? @Mapping(source="id", target="id") ^
I am trying to find a solution that does not require me to write a custom mapper for each record that implements my DateRuleDTO interface, because that would defeat the purpose of using mapstruct’s annotations in the first place. I have found a way to more or less accomplish this using a decorator with (imho) ugly reflection. I would like to see if I can get the result I am looking for (automatic mapping of each record’s id field) with annotations or minimal additional customization.
I should also mention that I am using lombok as well, and have included the lombok-mapstruct-binding dependency in my gradle build file:
implementation "org.mapstruct:mapstruct:1.5.5.Final"
annotationProcessor "org.mapstruct:mapstruct-processor:1.5.5.Final"
// https://mvnrepository.com/artifact/org.projectlombok/lombok-mapstruct-binding
implementation group: 'org.projectlombok', name: 'lombok-mapstruct-binding', version: '0.2.0'
annotationProcessor(group: 'org.projectlombok', name: 'lombok-mapstruct-binding', version: '0.2.0')
// If you are using mapstruct in test code
testAnnotationProcessor "org.mapstruct:mapstruct-processor:1.5.5.Final"
Thank you for any help!