Skip to content

@SubclassMapping not working with @Mapping nested properties and target all #3126

@bwgjoseph

Description

@bwgjoseph

Expected behavior

It should be able to map the nested properties from source to target subclass.

Usage

@SubclassMapping(source = HomeAddressDO.class, target = HomeAddressResponseDto.class)
@SubclassMapping(source = OfficeAddressDO.class, target = OfficeAddressResponseDto.class)
@Mapping(source = "auditable", target = ".")
AddressResponseDto toDto(AddressDO addressDo);

Actual behavior

MapStruct complains the following:

No property named "addressDo.auditable.createdBy" exists in source parameter(s). Did you mean "auditable"?

image

Steps to reproduce the problem

I have created a reproduce at here

In summary, this is the setup

public abstract class AddressDO {
    public abstract AddressDOBuilder<?, ?> toBuilder();

    private String id;
    private Auditable auditable;
}

public class Auditable {
    private String createdBy;
    private LocalDateTime createdAt;
}

public class HomeAddressDO extends AddressDO {
  // some fields
}

public class OfficeAddressDO extends AddressDO {
  // some fields
}

public interface AddressResponseDto {}

public record HomeAddressResponseDto(String id, String street, String postalCode, String unit, String createdBy, String createdAt) implements AddressResponseDto {}

public record OfficeAddressResponseDto(String id, String building, String street, String postalCode, String unit, String createdBy, String createdAt) implements AddressResponseDto {}

@Mapper(
    componentModel = MappingConstants.ComponentModel.SPRING,
    subclassExhaustiveStrategy = SubclassExhaustiveStrategy.RUNTIME_EXCEPTION
)
public interface AddressMapper {
    @SubclassMapping(source = HomeAddressDO.class, target = HomeAddressResponseDto.class)
    @SubclassMapping(source = OfficeAddressDO.class, target = OfficeAddressResponseDto.class)
    @Mapping(source = "auditable", target = ".") // < offending line
    AddressResponseDto toDto(AddressDO addressDo);
}

MapStruct Version

1.5.3.Final

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions