Skip to content

Mapping methods with several source parameters: Impossible to ignore attributes of an object used as parameter while it is directly used as source #3192

@tl3gr4nd

Description

@tl3gr4nd

Expected behavior

I expected MapStruct to ignore the attributes of the secondaryEntity:

import javax.annotation.processing.Generated;
import poc.dtos.Dto;
import poc.entities.MainEntity;
import poc.entities.SecondaryEntity;

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2023-03-10T14:33:10+0100",
    comments = "version: 1.5.3.Final, compiler: IncrementalProcessingEnvironment from gradle-language-java-8.0.2.jar, environment: Java 17.0.4 (Eclipse Adoptium)"
)
public class MainEntityMapperImpl implements MainEntityMapper {

    @Override
    public MainEntity map(Dto dto, SecondaryEntity secondaryEntity) {
        if ( dto == null && secondaryEntity == null ) {
            return null;
        }

        MainEntity mainEntity = new MainEntity();

        if ( dto != null ) {
            mainEntity.setMainEntityAttribute( dto.getDtoAttribute() );
        }
        if ( secondaryEntity != null ) {
            mainEntity.setOtherEntity( secondaryEntity );
//--- I expected MapStruct to not generate this line:
           mainEntity.setEntityCommonAttribute( secondaryEntity.getEntityCommonAttribute() );
//---
        }

        return mainEntity;
    }
}

Actual behavior

MapStruct generates the following implementation which maps an attribute of secondaryEntity to the attribute of the same name in mainEntity:

import javax.annotation.processing.Generated;
import poc.dtos.Dto;
import poc.entities.MainEntity;
import poc.entities.SecondaryEntity;

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2023-03-10T14:33:10+0100",
    comments = "version: 1.5.3.Final, compiler: IncrementalProcessingEnvironment from gradle-language-java-8.0.2.jar, environment: Java 17.0.4 (Eclipse Adoptium)"
)
public class MainEntityMapperImpl implements MainEntityMapper {

    @Override
    public MainEntity map(Dto dto, SecondaryEntity secondaryEntity) {
        if ( dto == null && secondaryEntity == null ) {
            return null;
        }

        MainEntity mainEntity = new MainEntity();

        if ( dto != null ) {
            mainEntity.setMainEntityAttribute( dto.getDtoAttribute() );
        }
        if ( secondaryEntity != null ) {
            mainEntity.setOtherEntity( secondaryEntity );
//---- HERE IS THE PROBLEM:
            mainEntity.setEntityCommonAttribute( secondaryEntity.getEntityCommonAttribute() );
//----
        }

        return mainEntity;
    }
}

Steps to reproduce the problem

package poc.dtos;

public class Dto {

  private String dtoAttribute;

  public String getDtoAttribute() {
    return dtoAttribute;
  }

  public void setDtoAttribute(String dtoAttribute) {
    this.dtoAttribute = dtoAttribute;
  }
}
package poc.entities;

public class MainEntity {

  private String mainEntityAttribute;
  private String entityCommonAttribute;
  private SecondaryEntity otherEntity;

  public String getMainEntityAttribute() {
    return mainEntityAttribute;
  }

  public void setMainEntityAttribute(String mainEntityAttribute) {
    this.mainEntityAttribute = mainEntityAttribute;
  }

  public String getEntityCommonAttribute() {
    return entityCommonAttribute;
  }

  public void setEntityCommonAttribute(String entityCommonAttribute) {
    this.entityCommonAttribute = entityCommonAttribute;
  }

  public SecondaryEntity getOtherEntity() {
    return otherEntity;
  }

  public void setOtherEntity(SecondaryEntity otherEntity) {
    this.otherEntity = otherEntity;
  }
}
package poc.entities;

public class SecondaryEntity {

  private String entityCommonAttribute;

  public String getEntityCommonAttribute() {
    return entityCommonAttribute;
  }

  public void setEntityCommonAttribute(String entityCommonAttribute) {
    this.entityCommonAttribute = entityCommonAttribute;
  }
}
package poc.mappers;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import poc.dtos.Dto;
import poc.entities.MainEntity;
import poc.entities.SecondaryEntity;

@Mapper
public interface MainEntityMapper {

  @Mapping(source = "dto.dtoAttribute", target = "mainEntityAttribute")
  @Mapping(source = "secondaryEntity", target = "otherEntity")
  @Mapping(source = "secondaryEntity", target = ".", ignore = true)
  MainEntity map(Dto dto, SecondaryEntity secondaryEntity);

}

MapStruct Version

MapStruct 1.5.3.Final

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