Skip to content

Mapstruct mapping incorrect properties when one of them is supporting adder #4038

Description

@altaiezior

Expected behavior

If I have 2 List mapped in a class with one of them having adder, then mapstruct should ideally make use adder for this property and setter for the other.

Actual behavior

Mapstruct is mapping both source proprties to this adder property, which is incorrect

@Value
@Builder
class RequestDto {
    String name;

    @Singular("addItem")
    List<String> items;

    List<String> moreItems;

    Set<String> setItems;
}

@Value
@Builder
class RequestBean {
    String name;
    List<String> items;
    List<String> moreItems;
    Set<String> setItems;
}


@Mapper(config = CentralConfig.class)
interface RequestMapper {
    RequestDto toDto(RequestBean requestBean);
}

// Generated Code

    @Override
    public RequestDto toDto(RequestBean requestBean) {
        if ( requestBean == null ) {
            return null;
        }

        RequestDto.RequestDtoBuilder requestDto = RequestDto.builder();

        if ( prechecksSupport.isNotBlank( requestBean.getName() ) ) {
            requestDto.name( requestBean.getName() );
        }
        if ( prechecksSupport.isNotEmpty( requestBean.getItems() ) ) {
            for ( String item : requestBean.getItems() ) {
                requestDto.addItem( item );
            }
        }
        if ( prechecksSupport.isNotEmpty( requestBean.getMoreItems() ) ) {
            for ( String moreItem : requestBean.getMoreItems() ) {
                requestDto.addItem( moreItem );
            }
        }

        return requestDto.build();
    }

If we see here, moreItems and setItems are not mapped properly

Steps to reproduce the problem

https://github.com/altaiezior/mapstruct-issues/tree/master/issue-4038

MapStruct Version

Mapstruct 1.5.5

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions