Search code examples
javamapstruct

Mapstructs abstract decorator used in other mapping results in ambiguous exception


I implemented a decorator to customize the mapping of an entity, let's say MappingDecoratorA, which is an abstract class and implements the MapperA interface of mapstructs.

public abstract class MappingDecoratorA implements MapperA {
...}
@Mapper
@DecoratedWith(MappingDecoratorA .class)
public interface MapperA {

In an other mapping I use the MapperA, let's say MappingB, which uses cdi

@Mapper(uses = { MapperA.class},
    componentModel = "cdi")
public interface MapperB{

Mapstructs generates two implementations for the MapperA, MapperAImpl and MapperAImpl_. In my situation the inject mechanism doesn't know which implementation to use. The result is an ambiguous exception listening the two implementation.

Does mapstruct support a solution for my problem?


Solution

  • When using the non default componentModel you have to use it for all the mappers. Especially if you want to reuse them. Otherwise the specific component won't know how to inject and create the mappers.

    So a solution for your problem would be to do

    @Mapper(componentModel = "cdi")
    @DecoratedWith(MappingDecoratorA .class)
    public interface MapperA {
    }