Skip to content

Do not return null when the return value is kown to be @Notnull #4104

Description

@pkernevez

Use case

With JSpecify, the generated code is not compliant with the contract of the method.
So the generated code can't be part of NullAway check.

This is the Mapper declaration we have with @NullMarked so both parameter and return value are not null:

null)")
    public abstract CommonInstrumentEmbeddedDto toEmbeddedDto(Instrument entity);

The target Dto, one nullable and one not nullable:

public record CommonInstrumentEmbeddedDto(
        String name,
        @Nullable String shortName
) {}

Generated source:

    @Override
    public CommonInstrumentEmbeddedDto toEmbeddedDto(IInstrument entity) {
        if ( entity == null ) {
            return null;
        }

        String name = null;
        String shortName = null;

        name = entity.getName();
        shortName = entity.getShortName();

        CommonInstrumentEmbeddedDto commonInstrumentEmbeddedDto = new CommonInstrumentEmbeddedDto( name, shortName);

        return commonInstrumentEmbeddedDto;
    }

Generated Code

NonNull returned:
When the return value is known to be not null, we should not have this part:

        if ( entity == null ) {
            return null;
        }

And map struct should probably fails during the mapping if the parameter could be null.

Possible workarounds

No response

MapStruct Version

1.7.0.Beta2

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions