Skip to content

JSpecify: Propagate nullability annotations to mapper implementation #4076

Open

Description

@RaniAgus

Use case

While trying the latest beta I've noticed that IDE adds a Overriding method parameters are not annotated warning to all null marked mappers because the generated implementations don't include any annotation at all:

Image

Generated Code

For example, here:

@Mapper
@NullMarked // or @NullMarked in package-info.java
public interface NullMarkedMapper {

    @Mapping(target = "foo", source = "source1.foo")
    @Mapping(target = "bar", source = "source1.bar")
    @Mapping(target = "baz", source = "source2.baz")
    Target map(Source1 source1, @Nullable Source2 source2);
}

Generated code could be:

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "0000-00-00T00:00:00+0000",
    comments = "version: , compiler: javac, environment: Java 21"
)
@NullMarked
public class NullMarkedMapperImpl implements NullMarkedMapper {

    @Override
    public Target map(Source1 source1, @Nullable Source2 source2) {

        Long baz = null;
        if ( source2 != null ) {
            baz = source2.getBaz();
        }
        String foo = null;
        foo = source1.getFoo();
        boolean bar = false;
        bar = source1.isBar();

        Target target = new Target( foo, bar, baz );

        return target;
    }
}

Possible workarounds

Right now I'm ignoring the warning because it's not a compilation issue

MapStruct Version

1.7.0.Beta2

Activity

  1. RaniAgus commented on Jun 28, 2026

    @RaniAgus
    ContributorAuthor

    Oops, might be related to #4036

  2. filiphr commented on Jun 28, 2026

    @filiphr
    Member

    Thanks for the feedback @RaniAgus. It's extremely valuable. This is indeed similar with #4036. However, that one is for the helper methods that we generate. I'll keep both and try to look for a potential solution to pass the annotations. My goal is to have no warnings from the tools, to truly be native to the use of JSpecify

  3. added this to the 1.7.0.RC1 milestone on Jun 29, 2026
  4. pkernevez commented on Aug 4, 2026

    @pkernevez

    As a temporary solution you can add @AnnotateWith(NullMarked.class) to your mapper.

  5. pkernevez commented on Aug 4, 2026

    @pkernevez

    In my case the parameter never have annotations.

    So the generated code is:

        @Override
        public Target map(Source1 source1, Source2 source2) {
    

    Instead of you sample:

        @Override
        public Target map(Source1 source1, @Nullable Source2 source2) {
    

    Is there a reason to not replicate all the parameters annotations ? (included non JSpecify ones)

  6. added a commit that references this issue on Aug 14, 2026
  7. self-assigned this
    on Sep 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions