mapstruct / mapstruct/mapstruct

@InheritConfiguration and @BeanMapping: ignoreUnmappedSourceProperties are not "merged" as expected

Open
#2,092 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

up-for-grabs
Dominant language
Java
Stars
7.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

First of all: mapstruct is awesome! I use it to map a really complex hibernate domain model to simple DTOs and vice versa - works great!

I work with a strict MapperConfig regarding source and target properties to catch future changes in the domain model:

@MapperConfig(
        unmappedSourcePolicy = ReportingPolicy.ERROR,
        unmappedTargetPolicy = ReportingPolicy.ERROR
)
public interface CentralConfig {}

For this reason, I have to work with @BeanMapping(ignoreUnmappedSourceProperties = {...}) a lot. Unfortunately this does not work well if @InheritConfiguration is used: The second method mapB "forgets" that the source property "a2" should be ignored, as defined for the first method mapA which configuration is inherited by mapB.

Am I doing something wrong or am I expecting something that does not (yet) work?

@Mapper(
        config = CentralConfig.class
)
public interface MyMapper {

    @Mapping(source = "a1", target = "aa1")
    @BeanMapping(ignoreUnmappedSourceProperties = {"a2"})
    TargetBeanA mapA(BeanA beanA);

    @InheritConfiguration
    @Mapping(source = "b1", target = "bb1") // inheritance works as expected
    @BeanMapping(ignoreUnmappedSourceProperties = {"b2"}) // compiler error: Unmapped source property: "a2"
    TargetBeanB mapB(BeanB beanB);
}

Below all the beans:

@Getter
@Setter
public class BeanA {
    
    private int a1;
    private int a2;
}
@Getter
@Setter
public class BeanB extends BeanA {

    private int b1;
    private int b2;
}
@Getter
@Setter
public class TargetBeanA {
    
    private int aa1;
}
@Getter
@Setter
public class TargetBeanB extends TargetBeanA {
    
    private int bb1;
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how @InheritConfiguration and @BeanMapping are processed for inherited mapping methods. Verify the behavior with the BeanA/BeanB and TargetBeanA/TargetBeanB example: the inherited a2 ignore and the local b2 ignore should both be honored without an unmapped-source error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.