Skip to content

MapStruct can not map only primitives #3317

@Zhenyria

Description

@Zhenyria

Expected behavior

My DTO:

public record SomeDto(int id, long number) {
}

I created the follow method for mapping:

SomeDto mapToDto(int id, long number);

I expect that something like this will be generated:

@Override
public SomeDto map(int id, long number) {
    int id1 = 0;
    id1 = id;
    long number1 = 0L;
    number1 = number;

    SomeDto someDto = new SomeDto( id1, number1 );

    return someDto;
}

Actual behavior

For some reason MapStruct tries to check passed parameters for null and it makes it everywhen. So i get the follow implementation of the map method:

@Override
public SomeDto map(int id, long number) {
    if (  ) {
        return null;
    }

    int id1 = 0;
    id1 = id;
    long number1 = 0L;
    number1 = number;

    SomeDto someDto = new SomeDto( id1, number1 );

    return someDto;
}

So there i get compilation error because the condition in the if statement is empty.

Steps to reproduce the problem

Java 17, Spring Boot 2.7.10

DTO:

public record SomeDto(int id, long number) {
}

Mapper:

@Mapper(componentModel = "spring")
public interface SomeMapper {
    SomeDto mapToDto(int id, long number);
}

MapStruct Version

1.5.5.Final

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions