Iterable : UnMapped nested properties

MAPSTRUCT

목록 보기
1/1

List를 List로 매핑하는 과정에서 조인 관계에 있는 entity의 field들만 매핑이 안되는 warning이 발생했습니다.


warning: Unmapped target properties: "displayName, ownerName". Mapping from Collection element "Comments comments" to "CommentListResponseDto commentListResponseDto".
  List<CommentListResponseDto> toListDto(List<Comments> comments);
  

빌드 과정에서 대충 이런 에러가 발생!

해당 mapperImpl에서도 두 필드릉 매핑하는 과정은 생략되어 있었습니다.

https://stackoverflow.com/a/73247962

위 스택오버플로우를 확인해 보니 내부 객체를 먼저 매핑 후 (이때 @Named("내부객체매핑")를 이용하기)
@IterableMapping()에 elementTargetType = Dto.class를 사용하는 것이 아닌
qualifiedByName = "내부객체매핑" 옵션을 이용하면 warning없이 매핑이 잘 되는 것을 확인할 수 있었습니다!

기존 mapper

@Mapper(componentModel = "spring", uses = {CommonMapper.class})
public interface CommentMapper {

  @IterableMapping(elementTargetType = CommentListResponseDto.class)
  @Mappings({
    @Mapping(source = "base.displayName", target = "displayName"),
    @Mapping(source = "base.name", target = "ownerName"),
    @Mapping(target = "cretaedDatetime", qualifiedByName = "CommonMapper, timestampToString")
  })
  List<CommentListResponseDto> toListDto(List<Comments> comments);

}

기존 mapperImle

public class CommentMapperImpl implements CommentMapper {
	...
    
    @Override
    public List<CommentListResponseDto> toListDto(List<Comments> comments) {
        if ( comments == null ) {
            return null;
        }

        List<CommentListResponseDto> list = new ArrayList<CommentListResponseDto>( comments.size() );
        for ( Comments comments1 : comments ) {
            list.add( commentsToCommentListResponseDto( comments1 ) );
        }

        return list;
    }
    
    ...
    
    protected CommentListResponseDto commentsToCommentListResponseDto(Comments comments) {
        if ( comments == null ) {
            return null;
        }

        CommentListResponseDto.CommentListResponseDtoBuilder commentListResponseDto = CommentListResponseDto.builder();

        commentListResponseDto.id( comments.getId() );
        commentListResponseDto.comment( comments.getComment() );
        commentListResponseDto.createdDatetime( xmlGregorianCalendarToString( dateToXmlGregorianCalendar( comments.getCreatedDatetime() ), null ) );
        // !!!!!displayName과 ownerName을 매핑해주는 로직 없음!!!!!

        return commentListResponseDto.build();
    }
}

변경 후
mapper

  @Named("toDto")
  @Mappings({
    @Mapping(source = "base.displayName", target = "displayName"),
    @Mapping(source = "base.name", target = "ownerName"),
      @Mapping(target = "createdDatetime", qualifiedByName = "timestampToString")
  })
  CommentListResponseDto toDto(Comments comment);

  @IterableMapping(qualifiedByName = "toDto")
  List<CommentListResponseDto> toListDto(List<Comments> comments);

mapperImpl

public class CommentMapperImpl implements CommentMapper {

    ...
    
    @Override
    public CommentListResponseDto toDto(Comments comment) {
        if ( comment == null ) {
            return null;
        }

        CommentListResponseDto.CommentListResponseDtoBuilder commentListResponseDto = CommentListResponseDto.builder();

        commentListResponseDto.displayName( commentBaseDisplayName( comment ) );
        commentListResponseDto.ownerName( commentBaseName( comment ) );
        commentListResponseDto.createdDatetime( commonMapper.timestampToString( comment.getCreatedDatetime() ) );
        commentListResponseDto.id( comment.getId() );
        commentListResponseDto.comment( comment.getComment() );

        return commentListResponseDto.build();
    }
    
    ...
    
    @Override
    public List<CommentListResponseDto> toListDto(List<Comments> comments) {
        if ( comments == null ) {
            return null;
        }

        List<CommentListResponseDto> list = new ArrayList<CommentListResponseDto>( comments.size() );
        for ( Comments comments1 : comments ) {
            list.add( toDto( comments1 ) );
        }

        return list;
    }
}
profile
야호 약간 헌 개발자....

1개의 댓글

comment-user-thumbnail
2025년 2월 4일

Be amazed by the fresh information updates. Experience many impressive and attractive features. Study to learn and improve specialized Geometry Dash skills.

답글 달기