mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#3018: Use MappingControl with SubclassMapping
This commit is contained in:
parent
e979f506fa
commit
608d476ed2
@ -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
|
||||
|
@ -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);
|
||||
}
|
@ -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);
|
||||
}
|
@ -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() {
|
||||
|
@ -15,4 +15,5 @@ public class Car extends Vehicle {
|
||||
public void setManual(boolean manual) {
|
||||
this.manual = manual;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user