Initial situation
Using current 1.5.0.Beta2 MapStruct release with JDK 13.
Domain Model
class Wrapper {
private Fruit fruit;
}
abstract class Fruit {
private int weight;
/* ... */
}
class Apple extends Fruit {
/* ... */
}
class Banana extends Fruit {
/* ... */
}
(Corresponding 1:1 DTOs omitted)
Mappers
Mapper for Wrapper class
@Mapper(uses = {FruitMapper.class})
public interface WrapperMapper {
WrapperDto map(Wrapper wrapper)
}
Mapper for Fruit class(es)
MapStruct Mapper for abstract Fruit class with method signature and annotations:
@Mapper(subclassExhaustiveStrategy = SubclassExhaustiveStrategy.RUNTIME_EXCEPTION /*...*/)
public interface FruitMapper {
@SubclassMapping(source = Apple.class, target = AppleDto.class)
@SubclassMapping(source = Banana.class, target = BananaDto.class)
FruitDto map(Fruit fruit);
}
Problem
The above works fine until a field needs to be ignored on the referenced abstract class (e.g. the weight of a Fruit).
Putting this annotation to the WrapperMapper map method...
@Mapping(target = "fruit.weight", ignore = true)
WrapperDto map(Wrapper wrapper)
...leads to The return type FruitDto is an abstract class or interface. Provide a non abstract / non interface result type or a factory method. compile error.
Question
Is there a way to skip fields in MapStruct mapping as outlined without getting this compile error?