Skip to content

Support @InheritConfiguration for @SubclassMapping in methods with identical signature #3125

@EvaristeGalois11

Description

@EvaristeGalois11

Expected behavior

Working on #3120 i realized that @SubclassMapping wasn't been inherited.

So in a mapper like this i was expecting that TestMapper#mapAnother will inherit all the annotations and basically behave the same way as TestMapper#map:

@Mapper
public interface TestMapper {
    TestMapper INSTANCE = Mappers.getMapper( TestMapper.class );

    @SubclassMapping(source = CarDto.class, target = Car.class)
    @SubclassMapping(source = BikeDto.class, target = Bike.class)
    @Mapping(source = "maker", target = "vehicleManufacturingCompany")
    Vehicle map(VehicleDto vehicle);

    @InheritConfiguration
    Vehicle mapAnother(VehicleDto vehicleDto);
}

Actual behavior

But mapstruct generate this, correctly inheriting the mapping annotation but leaving out the subclass related stuff:

public class TestMapperImpl implements TestMapper {

    @Override
    public Vehicle map(VehicleDto vehicle) {
        if ( vehicle == null ) {
            return null;
        }

        if (vehicle instanceof CarDto ) {
            return  carDtoToCar( (CarDto) vehicle );
        }
        else if (vehicle instanceof BikeDto ) {
            return  bikeDtoToBike( (BikeDto) vehicle );
        }
        else {
            Vehicle vehicle1 = new Vehicle();

            vehicle1.setVehicleManufacturingCompany( vehicle.getMaker() );
            vehicle1.setName( vehicle.getName() );

            return vehicle1;
        }
    }

    @Override
    public Vehicle mapAnother(VehicleDto vehicleDto) {
        if ( vehicleDto == null ) {
            return null;
        }

        Vehicle vehicle = new Vehicle();

        vehicle.setVehicleManufacturingCompany( vehicleDto.getMaker() );
        vehicle.setName( vehicleDto.getName() );

        return vehicle;
    }

    protected Car carDtoToCar(CarDto carDto) {
        if ( carDto == null ) {
            return null;
        }

        Car car = new Car();

        car.setVehicleManufacturingCompany( carDto.getMaker() );
        car.setName( carDto.getName() );
        car.setManual( carDto.isManual() );

        return car;
    }

    protected Bike bikeDtoToBike(BikeDto bikeDto) {
        if ( bikeDto == null ) {
            return null;
        }

        Bike bike = new Bike();

        bike.setVehicleManufacturingCompany( bikeDto.getMaker() );
        bike.setName( bikeDto.getName() );
        bike.setNumberOfGears( bikeDto.getNumberOfGears() );

        return bike;
    }
}

Steps to reproduce the problem

What the expected behavior says.

MapStruct Version

MapStruct 1.5.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