Skip to content

Mapping Composition does not work for @SubclassMapping #3174

@bwgjoseph

Description

@bwgjoseph

Expected behavior

I'm trying to use mapping-composition for @SubclassMapping

Coming from #2528 (comment) where @Zegveld mentioned that it should be supported.

Actual behavior

When creating the meta-annotation, it complains the following

The annotation @SubclassMapping cannot be repeated at this location since its container annotation type @SubclassMappings is disallowed at this location

image

image

Steps to reproduce the problem

I have the following setup

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
@JsonSubTypes({
    @Type(value = HomeAddressRequestDto.class, name = "home"),
    @Type(value = OfficeAddressRequestDto.class, name = "office")
})
public interface AddressRequestDto {}

@Builder
@JsonTypeName("home")
public record HomeAddressRequestDto(
        String id,
        String street,
        String postalCode,
        String unit,
        String createdBy,
        String createdAt,
        Country country,
        Point location)
        implements AddressRequestDto {}

@Builder
@JsonTypeName("office")
public record OfficeAddressRequestDto(String building, String street, String postalCode, String unit, String createdBy, String createdAt) implements AddressRequestDto {}

@Mapper(
        componentModel = MappingConstants.ComponentModel.SPRING,
        subclassExhaustiveStrategy = SubclassExhaustiveStrategy.RUNTIME_EXCEPTION
)
public interface AddressMapper {
    @SubclassMapping(source = HomeAddressRequestDto.class, target = HomeAddressDO.class)
    @SubclassMapping(source = OfficeAddressRequestDto.class, target = OfficeAddressDO.class)
    AddressDO toDomainObject(AddressRequestDto addressDto);

Which I am trying to use Mapping Composition

@Retention(RetentionPolicy.CLASS)
@SubclassMapping(source = HomeAddressRequestDto.class, target = HomeAddressDO.class)
@SubclassMapping(source = OfficeAddressRequestDto.class, target = OfficeAddressDO.class)
public @interface CombinedSubclassMapping {}

Then change the mapper class

public interface AddressMapper {
    @CombinedSubclassMapping
    AddressDO toDomainObject(AddressRequestDto addressDto);

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