#3360 Do not report unmapped source and target properties when result type is abstract due to runtime exception subclass exhaustive strategy (#3526)

This commit is contained in:
Filip Hrisafov 2024-02-11 19:53:38 +01:00 committed by GitHub
parent bb1cd63485
commit c374b5267f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 181 additions and 2 deletions

View File

@ -319,8 +319,10 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
handleUnmappedConstructorProperties();
// report errors on unmapped properties
reportErrorForUnmappedTargetPropertiesIfRequired();
reportErrorForUnmappedSourcePropertiesIfRequired();
if ( shouldHandledDefinedMappings ) {
reportErrorForUnmappedTargetPropertiesIfRequired();
reportErrorForUnmappedSourcePropertiesIfRequired();
}
reportErrorForMissingIgnoredSourceProperties();
reportErrorForUnusedSourceParameters();

View File

@ -0,0 +1,34 @@
/*
* 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.bugs._3360;
import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.SubclassExhaustiveStrategy;
import org.mapstruct.SubclassMapping;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
@Mapper(
subclassExhaustiveStrategy = SubclassExhaustiveStrategy.RUNTIME_EXCEPTION,
unmappedTargetPolicy = ReportingPolicy.ERROR,
unmappedSourcePolicy = ReportingPolicy.ERROR
)
public interface Issue3360Mapper {
Issue3360Mapper INSTANCE = Mappers.getMapper( Issue3360Mapper.class );
@SubclassMapping(target = VehicleDto.Car.class, source = Vehicle.Car.class)
VehicleDto map(Vehicle vehicle);
@Mapping(target = "model", source = "modelName")
@BeanMapping(ignoreUnmappedSourceProperties = "computedName")
VehicleDto.Car map(Vehicle.Car car);
}

View File

@ -0,0 +1,43 @@
/*
* 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.bugs._3360;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* @author Filip Hrisafov
*/
@IssueKey("3360")
@WithClasses({
Issue3360Mapper.class,
Vehicle.class,
VehicleDto.class,
})
class Issue3360Test {
@ProcessorTest
void shouldCompileWithoutErrorsAndWarnings() {
Vehicle vehicle = new Vehicle.Car( "Test", "car", 4 );
VehicleDto target = Issue3360Mapper.INSTANCE.map( vehicle );
assertThat( target.getName() ).isEqualTo( "Test" );
assertThat( target.getModel() ).isEqualTo( "car" );
assertThat( target ).isInstanceOfSatisfying( VehicleDto.Car.class, car -> {
assertThat( car.getNumOfDoors() ).isEqualTo( 4 );
} );
assertThatThrownBy( () -> Issue3360Mapper.INSTANCE.map( new Vehicle.Motorbike( "Test", "bike" ) ) )
.isInstanceOf( IllegalArgumentException.class )
.hasMessage( "Not all subclasses are supported for this mapping. Missing for " + Vehicle.Motorbike.class );
}
}

View File

@ -0,0 +1,55 @@
/*
* 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.bugs._3360;
/**
* @author Filip Hrisafov
*/
public abstract class Vehicle {
private final String name;
private final String modelName;
protected Vehicle(String name, String modelName) {
this.name = name;
this.modelName = modelName;
}
public String getName() {
return name;
}
public String getModelName() {
return modelName;
}
public String getComputedName() {
return null;
}
public static class Car extends Vehicle {
private final int numOfDoors;
public Car(String name, String modelName, int numOfDoors) {
super( name, modelName );
this.numOfDoors = numOfDoors;
}
public int getNumOfDoors() {
return numOfDoors;
}
}
public static class Motorbike extends Vehicle {
public Motorbike(String name, String modelName) {
super( name, modelName );
}
}
}

View File

@ -0,0 +1,45 @@
/*
* 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.bugs._3360;
/**
* @author Filip Hrisafov
*/
public abstract class VehicleDto {
private String name;
private String model;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public static class Car extends VehicleDto {
private int numOfDoors;
public int getNumOfDoors() {
return numOfDoors;
}
public void setNumOfDoors(int numOfDoors) {
this.numOfDoors = numOfDoors;
}
}
}