#3158 BeanMapping#ignoreByDefault should work properly for constructor properties

This commit is contained in:
Filip Hrisafov 2023-04-02 19:47:08 +02:00
parent c6ea69eaf9
commit e69843f46e
4 changed files with 83 additions and 7 deletions

View File

@ -285,7 +285,11 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
return null;
}
if ( !mappingReferences.isRestrictToDefinedMappings() ) {
boolean applyImplicitMappings = !mappingReferences.isRestrictToDefinedMappings();
if ( applyImplicitMappings ) {
applyImplicitMappings = beanMapping == null || !beanMapping.isignoreByDefault();
}
if ( applyImplicitMappings ) {
// apply name based mapping from a source reference
applyTargetThisMapping();
@ -1522,6 +1526,10 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
if ( mappingReferences.isForForgedMethods() ) {
return ReportingPolicyGem.IGNORE;
}
// If we have ignoreByDefault = true, unprocessed target properties are not an issue.
if ( method.getOptions().getBeanMapping().isignoreByDefault() ) {
return ReportingPolicyGem.IGNORE;
}
if ( method.getOptions().getBeanMapping() != null ) {
return method.getOptions().getBeanMapping().unmappedTargetPolicy();
}

View File

@ -30,12 +30,6 @@ public class Issue2149Test {
line = 18,
message = "Using @BeanMapping( ignoreByDefault = true ) with @Mapping( target = \".\", ... ) is not " +
"allowed. You'll need to explicitly ignore the target properties that should be ignored instead."
),
@Diagnostic(
type = Erroneous2149Mapper.class,
kind = javax.tools.Diagnostic.Kind.WARNING,
line = 20,
message = "Unmapped target property: \"address\"."
)
}
)

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._3158;
import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
@Mapper
public interface Issue3158Mapper {
Issue3158Mapper INSTANCE = Mappers.getMapper( Issue3158Mapper.class );
@BeanMapping(ignoreByDefault = true)
@Mapping(target = "name")
Target map(Target target);
class Target {
private final String name;
private final String email;
public Target(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
}
}

View File

@ -0,0 +1,31 @@
/*
* 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._3158;
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;
/**
* @author Filip Hrisafov
*/
@WithClasses(Issue3158Mapper.class)
@IssueKey("3158")
class Issue3158Test {
@ProcessorTest
void beanMappingIgnoreByDefaultShouldBeRespectedForConstructorProperties() {
Issue3158Mapper.Target target = Issue3158Mapper.INSTANCE.map( new Issue3158Mapper.Target(
"tester",
"tester@test.com"
) );
assertThat( target.getName() ).isEqualTo( "tester" );
assertThat( target.getEmail() ).isNull();
}
}