Skip to content

JSpecify: Missing compile error when reused method returns @Nullable to @NonNull constructor #4081

Description

@RaniAgus

Expected behavior

Missing compile error when reused method returns @Nullable but constructor expects @NonNull parameter

Given:

@Mapper
@NullMarked
public interface ErroneousIssue4081Mapper {
    Target map(Source source);

    default Target.@Nullable Payload mapPayload(String payload) {
        if ( payload.isBlank() ) {
            return null;
        }
        return new Target.Payload( payload );
    }
}

public class Source {
    private final String payload;

    public @NonNull String getPayload() { return this.payload; }
}

public class Target {
    private final Payload payload;
    public Target(@NonNull Payload payload) { this.payload = payload; }

    public class Payload {
        private final String value;
        public Payload(@NonNull String value) { this.value = value; }
    }
}

Expected: compilation fails with

error: Can't map potentially nullable source property "payload" to @NonNull constructor parameter "payload".

Actual behavior

Compilation succeeds silently:

@Generated
public class ErroneousIssue4081MapperImpl implements ErroneousIssue4081Mapper {

    @Override
    public Target map(Source source) {

        Target.Payload payload = null;

        payload = mapValue( source.getPayload() );

        Target target = new Target( payload );

        return target;
    }
}

Steps to reproduce the problem

A @NullMarked mapper where a reused method has an explicit @Nullable return type, and the target constructor parameter is @NonNull.

Can be reproduced in this branch: main...RaniAgus:mapstruct:issues/4081

MapStruct Version

1.7.0.Beta2

Activity

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