mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2560 Ignore source properties if ignoreByDefault = true
This commit is contained in:
parent
7064e0bc97
commit
9ed4e389f8
@ -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
|
* 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).
|
* @return The ignore strategy (default false).
|
||||||
*
|
*
|
||||||
|
@ -1514,6 +1514,10 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
|||||||
if ( mappingReferences.isForForgedMethods() ) {
|
if ( mappingReferences.isForForgedMethods() ) {
|
||||||
return ReportingPolicyGem.IGNORE;
|
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();
|
return method.getOptions().getMapper().unmappedSourcePolicy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
}
|
@ -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() {
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user