#3609 Pass bean mapping ignored unmapped source properties to subclass forged methods

Co-authored-by: thunderhook <8238759+thunderhook@users.noreply.github.com>
This commit is contained in:
Obolrom 2024-07-20 15:06:49 +03:00 committed by GitHub
parent 66f4288842
commit df49ce5ff9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 109 additions and 2 deletions

View File

@ -146,13 +146,30 @@ public class ForgedMethod implements Method {
basedOn,
history,
mappingReferences == null ? MappingReferences.empty() : mappingReferences,
forgedNameBased
forgedNameBased,
MappingMethodOptions.getSubclassForgedMethodInheritedOptions( basedOn.getOptions() )
);
}
private ForgedMethod(String name, Type sourceType, Type returnType, List<Parameter> additionalParameters,
Method basedOn, ForgedMethodHistory history, MappingReferences mappingReferences,
boolean forgedNameBased) {
this(
name,
sourceType,
returnType,
additionalParameters,
basedOn,
history,
mappingReferences,
forgedNameBased,
MappingMethodOptions.getForgedMethodInheritedOptions( basedOn.getOptions() )
);
}
private ForgedMethod(String name, Type sourceType, Type returnType, List<Parameter> additionalParameters,
Method basedOn, ForgedMethodHistory history, MappingReferences mappingReferences,
boolean forgedNameBased, MappingMethodOptions options) {
// establish name
String sourceParamSafeName;
@ -185,7 +202,7 @@ public class ForgedMethod implements Method {
this.mappingReferences = mappingReferences;
this.forgedNameBased = forgedNameBased;
this.options = MappingMethodOptions.getForgedMethodInheritedOptions( basedOn.getOptions() );
this.options = options;
}
/**

View File

@ -66,6 +66,17 @@ public class BeanMappingOptions extends DelegatingOptions {
return options;
}
public static BeanMappingOptions forSubclassForgedMethods(BeanMappingOptions beanMapping) {
return new BeanMappingOptions(
beanMapping.selectionParameters != null ?
SelectionParameters.withoutResultType( beanMapping.selectionParameters ) : null,
beanMapping.ignoreUnmappedSourceProperties,
beanMapping.beanMapping,
beanMapping
);
}
public static BeanMappingOptions empty(DelegatingOptions delegatingOptions) {
return new BeanMappingOptions( null, Collections.emptyList(), null, delegatingOptions );
}

View File

@ -398,4 +398,17 @@ public class MappingMethodOptions {
null );
}
public static MappingMethodOptions getSubclassForgedMethodInheritedOptions(MappingMethodOptions options) {
return new MappingMethodOptions(
options.mapper,
options.mappings,
options.iterableMapping,
options.mapMapping,
BeanMappingOptions.forSubclassForgedMethods( options.beanMapping ),
options.enumMappingOptions,
options.valueMappings,
Collections.emptySet(),
null );
}
}

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._3609;
import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.SubclassMapping;
@Mapper(unmappedSourcePolicy = ReportingPolicy.ERROR)
public abstract class Issue3609Mapper {
@SubclassMapping(source = CarDto.class, target = Car.class)
@BeanMapping(ignoreUnmappedSourceProperties = "id")
public abstract Vehicle toVehicle(VehicleDto vehicle);
//CHECKSTYLE:OFF
public static class Vehicle {
public int price;
}
public static class Car extends Vehicle {
public int seats;
public Car(int price, int seats) {
this.price = price;
this.seats = seats;
}
}
public static class VehicleDto {
public int id;
public int price;
}
public static class CarDto extends VehicleDto {
public int seats;
}
//CHECKSTYLE:ON
}

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.bugs._3609;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
/**
* @author Roman Obolonskyii
*/
@IssueKey("3609")
@WithClasses(Issue3609Mapper.class)
public class Issue3609Test {
@ProcessorTest
void shouldCompileWithoutErrors() {
}
}