#3018: Use MappingControl with SubclassMapping

This commit is contained in:
Zegveld 2022-09-29 21:35:51 +02:00 committed by GitHub
parent e979f506fa
commit 608d476ed2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 1 deletions

View File

@ -405,7 +405,7 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
Collections.emptyList(),
subclassMappingOptions.getTarget(),
ctx.getTypeUtils() ).withSourceRHS( rightHandSide ),
null,
subclassMappingOptions.getMappingControl( ctx.getElementUtils() ),
null,
false );
Assignment assignment = ctx

View File

@ -0,0 +1,23 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.subclassmapping;
import org.mapstruct.Mapper;
import org.mapstruct.SubclassMapping;
import org.mapstruct.ap.test.subclassmapping.mappables.Bike;
import org.mapstruct.ap.test.subclassmapping.mappables.Car;
import org.mapstruct.ap.test.subclassmapping.mappables.Vehicle;
import org.mapstruct.control.DeepClone;
import org.mapstruct.factory.Mappers;
@Mapper(mappingControl = DeepClone.class)
public interface DeepCloneMapper {
DeepCloneMapper INSTANCE = Mappers.getMapper( DeepCloneMapper.class );
@SubclassMapping( source = Car.class, target = Car.class )
@SubclassMapping( source = Bike.class, target = Bike.class )
Vehicle map(Vehicle vehicle);
}

View File

@ -0,0 +1,26 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.subclassmapping;
import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
import org.mapstruct.NullValueMappingStrategy;
import org.mapstruct.SubclassMapping;
import org.mapstruct.ap.test.subclassmapping.mappables.Bike;
import org.mapstruct.ap.test.subclassmapping.mappables.Car;
import org.mapstruct.ap.test.subclassmapping.mappables.Vehicle;
import org.mapstruct.control.DeepClone;
import org.mapstruct.factory.Mappers;
@Mapper
public interface DeepCloneMethodMapper {
DeepCloneMethodMapper INSTANCE = Mappers.getMapper( DeepCloneMethodMapper.class );
@SubclassMapping( source = Car.class, target = Car.class )
@SubclassMapping( source = Bike.class, target = Bike.class )
@BeanMapping( mappingControl = DeepClone.class, nullValueMappingStrategy = NullValueMappingStrategy.RETURN_NULL )
Vehicle map(Vehicle vehicle);
}

View File

@ -52,6 +52,36 @@ public class SubclassMappingTest {
.containsExactly( CarDto.class, BikeDto.class );
}
@ProcessorTest
@WithClasses( DeepCloneMapper.class )
void deepCloneMappingClonesObjects() {
Car car = new Car();
car.setManual( true );
car.setName( "namedCar" );
car.setVehicleManufacturingCompany( "veMac" );
Vehicle result = DeepCloneMapper.INSTANCE.map( car );
assertThat( result ).isInstanceOf( Car.class );
assertThat( result ).isNotSameAs( car );
assertThat( result ).usingRecursiveComparison().isEqualTo( car );
}
@ProcessorTest
@WithClasses( DeepCloneMethodMapper.class )
void deepCloneMappingOnMethodClonesObjects() {
Car car = new Car();
car.setManual( true );
car.setName( "namedCar" );
car.setVehicleManufacturingCompany( "veMac" );
Vehicle result = DeepCloneMethodMapper.INSTANCE.map( car );
assertThat( result ).isInstanceOf( Car.class );
assertThat( result ).isNotSameAs( car );
assertThat( result ).usingRecursiveComparison().isEqualTo( car );
}
@ProcessorTest
@WithClasses( SimpleSubclassMapper.class )
void inverseMappingIsDoneUsingSubclassMapping() {

View File

@ -15,4 +15,5 @@ public class Car extends Vehicle {
public void setManual(boolean manual) {
this.manual = manual;
}
}