Skip to content

Overloaded AfterMapping is called twice #3849

@gbrehmer

Description

@gbrehmer

Expected behavior

If you have a class with hierarchy and multiple AfterMapping methods with the same name but for different level of the hierarchy, mapstruct should generate only one method call, because the overloaded method can not be reached

Actual behavior

Mapstruct handles both methods individual and did not recognize that the overloaded method can not be reached. It results in a duplicated call to the same method

Steps to reproduce the problem

public class Entity {
  long id;
  String name;
}

public interface ParentDTOInterface {
  long getId();
}

@Data
public class ParentDTO implements ParentDTOInterface {
  private long id;
}

@Data
public class ChildDTO extends ParentDTO {
  private String name;
}


public interface Mapper {

  ParentDTO mapParent(Entity entity);
  ChildDTO mapChild(Entity entity);
  
  @AfterMapping
  default void after(@MappingTarget ParentDTOInterface dto, Entity entity) {
  }
  
  @AfterMapping
  default void after(@MappingTarget ChildDTO childDTO, Entity entity) {
  }
}

generated child related code:

@Override
ChildDTO map(Entity entity) {
  ChildDTO childDTO = new ChildDTO();

  childDTO.setId(entity.getId());
  childDTO.setName(entity.getName());

  after(childDTO, entity);
  after(childDTO, entity); <---- this is the bug :)
}

generated parent related code:

@Override
ParentDTO map(Entity entity) {
  ParentDTO parentDTO = new ParentDTO();

  parentDTO.setId(entity.getId());

  after(parentDTO, entity);
}

MapStruct Version

1.6.3

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