Skip to content

Add support for custom exception for subclass exhaustive strategy for @SubclassMapping mapping #3821

@s-jepsen

Description

@s-jepsen

Use case

Introduce a new feature that can customize the IllegalArgumentException exception thrown, when a unexpected SubClassMapping is encountered.

Like the EnumMapping#unexpectedValueMappingException used to customize EnumMapping exception

Mapping example:

@SubclassMapping(target = GroupSpecificationEntity.class, source = GroupSpecification.class)
@SubclassMapping(target = SiteSpecificationEntity.class, source = SiteSpecification.class)
LocationSpecificationEntity map(LocationSpecification source);

Generated code:

@Override
    public Target map(Source source) {
        if ( source == null ) {
            return null;
        }

        if (source instanceof GroupSpecification) {
            return groupSpecificationToGroupSpecificationEntity( (GroupSpecification) source );
        }
        else if (source instanceof SiteSpecification) {
            return siteSpecificationToSiteSpecificationEntity( (SiteSpecification) source );
        }
        else {
            throw new IllegalArgumentException("Not all subclasses are supported for this mapping. Missing for " + source.getClass());
        }
    }

Generated Code

No response

Possible workarounds

No response

MapStruct Version

Mapstruct 1.6.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions