Skip to content

SubclassMapping generates an else branch for a sealed interface when the target is not abstract #3836

@mecseid

Description

@mecseid

Expected behavior

Generates the method implementation without any error, and creates the required if/else or switch/case statements.

Actual behavior

Drops an exception with unmapped target properties.
If it is set to WARN, the generated code contains an else branch with default mapping.

    @Override
    public TestMapping.To map(TestMapping.From from) {
        if ( from == null ) {
            return null;
        }

        if (from instanceof TestMapping.FromA) {
            return map( (TestMapping.FromA) from );
        }
        else if (from instanceof TestMapping.FromB) {
            return map( (TestMapping.FromB) from );
        }
        else {

            int to = 0;

            TestMapping.To to1 = new TestMapping.To( to );

            return to1;
        }
    }

Steps to reproduce the problem

Create an interface with more than one permitted class, then create a mapper with SubclassMapping, like:

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.SubclassMapping;

@Mapper(unmappedTargetPolicy = ReportingPolicy.WARN)
public interface TestMapping {

  sealed interface From permits FromA, FromB {}

  record FromA(int a) implements From {}

  record FromB(int b) implements From {}

  record To(int to) {}

  @Mapping(source = "a", target = "to")
  To map(FromA from);

  @Mapping(source = "b", target = "to")
  To map(FromB fromB);

  @SubclassMapping(source = FromA.class, target = To.class)
  @SubclassMapping(source = FromB.class, target = To.class)
  To map(From from);

}

It doesn't matter whether I set the concrete mapping for SubclassMapping with qualifiedBy/qualifiedByName or not.

Besides that, it looks like this bug depends on whether the target mapping is a concrete class/record or not (abstract or interface).

MapStruct Version

1.6.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions