Mapstruct: ignored MapperConfig

Viewed 416

I've two MapperConfig:

@MapperConfig(
    uses = {
        StringTypeMapper.class,
        ExtensionMapper.class
    }
)
public interface ElementMapperConfig extends GenericMapperConfig {
    
    @Mapping(target = "id", source = "idElement")
    @Mapping(target = "extension", source = "extension")
    Element mapElement(org.hl7.fhir.r4.model.Element fhir);

}

And GenericMapperConfig:

@MapperConfig(
    componentModel = "spring",
    injectionStrategy = InjectionStrategy.CONSTRUCTOR,
    nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
    nullValueMappingStrategy = NullValueMappingStrategy.RETURN_NULL
)
public interface GenericMapperConfig {

}

As you can see, I'm using spring component model.

Nevertheless, Mapper implementation is getting required Mapper using Mappers.getMapper(...).

As you can see, ElementMapperConfig extends GenericMapperConfig, but it seems that configuration from GenericMapperConfig is ignored.

Generated Mapper example:

@Generated(
    value = "org.mapstruct.ap.MappingProcessor"
)
public class StringTypeMapperImpl implements StringTypeMapper {

    private final ExtensionMapper extensionMapper = Mappers.getMapper( ExtensionMapper.class );

}

StringTypeMapper is:

@Mapper(
    config = ElementMapperConfig.class
)
public interface StringTypeMapper {

    @InheritConfiguration(name = "mapElement")
    StringType fhirToMpi(org.hl7.fhir.r4.model.StringType stringType);

}

I don't quite figure out why GenericMapperConfig configuration is not populated, I mean, I don't get why componentModel = "spring" is ignored on Mapper implementation.

1 Answers

The documentation does not mention this way of composing multiple MapperConfigurations.

It also does not mention another way of doing it, but this one works. The idea is to extend mappers instead of configurations.

Introduce a base mapper with generic configuration:

@Mapper(
    config = GenericMapperConfig.class
)
public interface BaseMapper {
}

Base your concrete mapper on the base one and configure it using the specific configuration:

@Mapper(
    config = ElementMapperConfig.class
)
public interface StringTypeMapper extends BaseMapper {
    @InheritConfiguration(name = "mapElement")
    StringType fhirToMpi(org.hl7.fhir.r4.model.StringType stringType);
}

Finally make ElementMapperConfig not inherit GenericMapperConfig:

@MapperConfig(
    uses = {
        StringTypeMapper.class,
        ExtensionMapper.class
    }
)
public interface ElementMapperConfig {
    @Mapping(target = "id", source = "idElement")
    @Mapping(target = "extension", source = "extension")
    Element mapElement(org.hl7.fhir.r4.model.Element fhir);
}
Related