#2560 Ignore source properties if ignoreByDefault = true

This commit is contained in:
Tobias Meggendorfer 2021-08-30 16:40:09 +02:00 committed by Filip Hrisafov
parent 7064e0bc97
commit 9ed4e389f8
7 changed files with 132 additions and 1 deletions

View File

@ -118,7 +118,7 @@ public @interface BeanMapping {
/**
* Default ignore all mappings. All mappings have to be defined manually. No automatic mapping will take place. No
* warning will be issued on missing target properties.
* warning will be issued on missing source or target properties.
*
* @return The ignore strategy (default false).
*

View File

@ -1514,6 +1514,10 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
if ( mappingReferences.isForForgedMethods() ) {
return ReportingPolicyGem.IGNORE;
}
// If we have ignoreByDefault = true, unprocessed source properties are not an issue.
if ( method.getOptions().getBeanMapping().isignoreByDefault() ) {
return ReportingPolicyGem.IGNORE;
}
return method.getOptions().getMapper().unmappedSourcePolicy();
}

View File

@ -0,0 +1,21 @@
/*
* 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.ignorebydefaultsource;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
@Mapper(
unmappedTargetPolicy = ReportingPolicy.IGNORE,
unmappedSourcePolicy = ReportingPolicy.ERROR)
public interface ErroneousSourceTargetMapper {
ErroneousSourceTargetMapper INSTANCE = Mappers.getMapper( ErroneousSourceTargetMapper.class );
@Mapping(source = "one", target = "one")
Target sourceToTarget(Source source);
}

View File

@ -0,0 +1,38 @@
/*
* 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.ignorebydefaultsource;
import javax.tools.Diagnostic.Kind;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
@IssueKey("2560")
public class IgnoreByDefaultSourcesTest {
@ProcessorTest
@WithClasses({ SourceTargetMapper.class, Source.class, Target.class })
public void shouldSucceed() {
}
@ProcessorTest
@WithClasses({ ErroneousSourceTargetMapper.class, Source.class, Target.class })
@ExpectedCompilationOutcome(
value = CompilationResult.FAILED,
diagnostics = {
@Diagnostic(type = ErroneousSourceTargetMapper.class,
kind = Kind.ERROR,
line = 20,
message = "Unmapped source property: \"other\".")
}
)
public void shouldRaiseErrorDueToNonIgnoredSourceProperty() {
}
}

View File

@ -0,0 +1,27 @@
/*
* 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.ignorebydefaultsource;
class Source {
private int one;
private int other;
public int getOne() {
return one;
}
public void setOne(int one) {
this.one = one;
}
public int getOther() {
return other;
}
public void setOther(int other) {
this.other = other;
}
}

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.ignorebydefaultsource;
import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
@Mapper(
unmappedTargetPolicy = ReportingPolicy.IGNORE,
unmappedSourcePolicy = ReportingPolicy.ERROR)
public interface SourceTargetMapper {
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
@Mapping(source = "one", target = "one")
@BeanMapping(ignoreByDefault = true)
Target sourceToTarget(Source source);
}

View File

@ -0,0 +1,18 @@
/*
* 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.ignorebydefaultsource;
class Target {
private int one;
public int getOne() {
return one;
}
public void setOne(int one) {
this.one = one;
}
}