#2949 Do not inverse inherit BeanMapping#ignoreUnmappedSourceProperties

This commit is contained in:
Filip Hrisafov 2022-08-24 18:36:43 +02:00 committed by GitHub
parent 3e0c62ac36
commit ef4c26b075
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 4 deletions

View File

@ -34,6 +34,7 @@ import org.mapstruct.tools.gem.GemValue;
public class BeanMappingOptions extends DelegatingOptions {
private final SelectionParameters selectionParameters;
private final List<String> ignoreUnmappedSourceProperties;
private final BeanMappingGem beanMapping;
/**
@ -46,6 +47,7 @@ public class BeanMappingOptions extends DelegatingOptions {
public static BeanMappingOptions forInheritance(BeanMappingOptions beanMapping) {
BeanMappingOptions options = new BeanMappingOptions(
SelectionParameters.forInheritance( beanMapping.selectionParameters ),
Collections.emptyList(),
beanMapping.beanMapping,
beanMapping
);
@ -57,7 +59,7 @@ public class BeanMappingOptions extends DelegatingOptions {
TypeUtils typeUtils, TypeFactory typeFactory
) {
if ( beanMapping == null || !isConsistent( beanMapping, method, messager ) ) {
BeanMappingOptions options = new BeanMappingOptions( null, null, mapperOptions );
BeanMappingOptions options = new BeanMappingOptions( null, Collections.emptyList(), null, mapperOptions );
return options;
}
@ -77,6 +79,7 @@ public class BeanMappingOptions extends DelegatingOptions {
//TODO Do we want to add the reporting policy to the BeanMapping as well? To give more granular support?
BeanMappingOptions options = new BeanMappingOptions(
selectionParameters,
beanMapping.ignoreUnmappedSourceProperties().get(),
beanMapping,
mapperOptions
);
@ -104,10 +107,12 @@ public class BeanMappingOptions extends DelegatingOptions {
}
private BeanMappingOptions(SelectionParameters selectionParameters,
List<String> ignoreUnmappedSourceProperties,
BeanMappingGem beanMapping,
DelegatingOptions next) {
super( next );
this.selectionParameters = selectionParameters;
this.ignoreUnmappedSourceProperties = ignoreUnmappedSourceProperties;
this.beanMapping = beanMapping;
}
@ -188,9 +193,7 @@ public class BeanMappingOptions extends DelegatingOptions {
}
public List<String> getIgnoreUnmappedSourceProperties() {
return Optional.ofNullable( beanMapping ).map( BeanMappingGem::ignoreUnmappedSourceProperties )
.map( GemValue::get )
.orElse( Collections.emptyList() );
return ignoreUnmappedSourceProperties;
}
public AnnotationMirror getMirror() {

View File

@ -0,0 +1,59 @@
/*
* 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._2949;
import org.mapstruct.BeanMapping;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
@Mapper(unmappedSourcePolicy = ReportingPolicy.ERROR)
public interface Issue2949Mapper {
Issue2949Mapper INSTANCE = Mappers.getMapper( Issue2949Mapper.class );
@Mapping( target = "property1", ignore = true)
@InheritInverseConfiguration
Source toSource(Target target);
@BeanMapping(ignoreUnmappedSourceProperties = { "property1" })
Target toTarget(Source source);
class Target {
private final String value;
public Target(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
class Source {
private final String value;
private final String property1;
public Source(String value, String property1) {
this.value = value;
this.property1 = property1;
}
public String getValue() {
return value;
}
public String getProperty1() {
return property1;
}
}
}

View File

@ -0,0 +1,35 @@
/*
* 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._2949;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Filip Hrisafov
*/
@WithClasses({
Issue2949Mapper.class
})
class Issue2949Test {
@ProcessorTest
void shouldCorrectlyInheritInverseBeanMappingWithIgnoreUnmappedSourceProeprties() {
Issue2949Mapper.Target target = Issue2949Mapper.INSTANCE.toTarget( new Issue2949Mapper.Source(
"test",
"first"
) );
assertThat( target.getValue() ).isEqualTo( "test" );
Issue2949Mapper.Source source = Issue2949Mapper.INSTANCE.toSource( target );
assertThat( source.getValue() ).isEqualTo( "test" );
assertThat( source.getProperty1() ).isNull();
}
}