Skip to content

Commit

Permalink
mapstruct#3360 Do not report unmapped source and target properties wh…
Browse files Browse the repository at this point in the history
…en result type is abstract due to runtime exception subclass exhaustive strategy
  • Loading branch information
filiphr committed Feb 11, 2024
1 parent ca1fd0d commit 0c61091
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,10 @@ else if ( !method.isUpdateMethod() ) {
handleUnmappedConstructorProperties();

// report errors on unmapped properties
reportErrorForUnmappedTargetPropertiesIfRequired();
reportErrorForUnmappedSourcePropertiesIfRequired();
if ( shouldHandledDefinedMappings ) {
reportErrorForUnmappedTargetPropertiesIfRequired();
reportErrorForUnmappedSourcePropertiesIfRequired();
}
reportErrorForMissingIgnoredSourceProperties();
reportErrorForUnusedSourceParameters();

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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 );
}
}
Original file line number Diff line number Diff line change
@@ -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 );
}

}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}

}

0 comments on commit 0c61091

Please sign in to comment.