Skip to content

@SubclassMapping order

First time using MapStruct (1.5.0.Beta2)

Say I have the following class hierarchy: C extends B extends A and Cdto extends Bdto extends Adto. And the following mapper:

@Mapper(componentModel = "spring", subclassExhaustiveStrategy = RUNTIME_EXCEPTION)
public interface MyMapper{
    @SubclassMapping(source = B.class, target = Bdto.class)
    @SubclassMapping(source = C.class, target = Cdto.class)
    Adto map(A source);
}

When I map a list of C objects I actually get a list of Bdtos. If however I change the ordering to:

@Mapper(componentModel = "spring", subclassExhaustiveStrategy = RUNTIME_EXCEPTION)
public interface MyMapper{
    @SubclassMapping(source = C.class, target = Cdto.class)
    @SubclassMapping(source = B.class, target = Bdto.class)
    Adto map(A source);
}

I get a list of Cdtos as expected. Is this by design? Is there any way to make it less dependent on annotation order?

Answer

This is by design. The reason for this is to let the user control the order for the mappings. The same behavior is used for @Mapping annotations.

Your first example should also get a compiler warning, although it might refer to the wrong type (target instead of source) at the moment. This should be fixed in the next release.