mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2132 Add unmappedTargetPolicy to @BeanMapping
This commit is contained in:
parent
4576103752
commit
cc1562c5ad
@ -140,6 +140,17 @@ public @interface BeanMapping {
|
|||||||
*/
|
*/
|
||||||
String[] ignoreUnmappedSourceProperties() default {};
|
String[] ignoreUnmappedSourceProperties() default {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* How unmapped properties of the target type of a mapping should be reported.
|
||||||
|
* If no policy is configured, the policy given via {@link MapperConfig#unmappedTargetPolicy()} or
|
||||||
|
* {@link Mapper#unmappedTargetPolicy()} will be applied, using {@link ReportingPolicy#WARN} by default.
|
||||||
|
*
|
||||||
|
* @return The reporting policy for unmapped target properties.
|
||||||
|
*
|
||||||
|
* @since 1.5
|
||||||
|
*/
|
||||||
|
ReportingPolicy unmappedTargetPolicy() default ReportingPolicy.WARN;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The information that should be used for the builder mappings. This can be used to define custom build methods
|
* The information that should be used for the builder mappings. This can be used to define custom build methods
|
||||||
* for the builder strategy that one uses.
|
* for the builder strategy that one uses.
|
||||||
|
@ -245,6 +245,7 @@ Supported values are:
|
|||||||
* `IGNORE`: unmapped target properties are ignored
|
* `IGNORE`: unmapped target properties are ignored
|
||||||
|
|
||||||
If a policy is given for a specific mapper via `@Mapper#unmappedTargetPolicy()`, the value from the annotation takes precedence.
|
If a policy is given for a specific mapper via `@Mapper#unmappedTargetPolicy()`, the value from the annotation takes precedence.
|
||||||
|
If a policy is given for a specific bean mapping via `@BeanMapping#unmappedTargetPolicy()`, it takes precedence over both `@Mapper#unmappedTargetPolicy()` and the option.
|
||||||
|`WARN`
|
|`WARN`
|
||||||
|===
|
|===
|
||||||
|
|
||||||
|
@ -1390,6 +1390,9 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
|||||||
if ( mappingReferences.isForForgedMethods() ) {
|
if ( mappingReferences.isForForgedMethods() ) {
|
||||||
return ReportingPolicyGem.IGNORE;
|
return ReportingPolicyGem.IGNORE;
|
||||||
}
|
}
|
||||||
|
if ( method.getOptions().getBeanMapping() != null ) {
|
||||||
|
return method.getOptions().getBeanMapping().unmappedTargetPolicy();
|
||||||
|
}
|
||||||
return method.getOptions().getMapper().unmappedTargetPolicy();
|
return method.getOptions().getMapper().unmappedTargetPolicy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@ import java.util.Objects;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import javax.lang.model.element.AnnotationMirror;
|
import javax.lang.model.element.AnnotationMirror;
|
||||||
import javax.lang.model.element.ExecutableElement;
|
import javax.lang.model.element.ExecutableElement;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.internal.gem.ReportingPolicyGem;
|
||||||
import org.mapstruct.ap.internal.util.ElementUtils;
|
import org.mapstruct.ap.internal.util.ElementUtils;
|
||||||
import org.mapstruct.ap.internal.util.TypeUtils;
|
import org.mapstruct.ap.internal.util.TypeUtils;
|
||||||
|
|
||||||
@ -88,6 +90,7 @@ public class BeanMappingOptions extends DelegatingOptions {
|
|||||||
&& !gem.nullValueCheckStrategy().hasValue()
|
&& !gem.nullValueCheckStrategy().hasValue()
|
||||||
&& !gem.nullValuePropertyMappingStrategy().hasValue()
|
&& !gem.nullValuePropertyMappingStrategy().hasValue()
|
||||||
&& !gem.nullValueMappingStrategy().hasValue()
|
&& !gem.nullValueMappingStrategy().hasValue()
|
||||||
|
&& !gem.unmappedTargetPolicy().hasValue()
|
||||||
&& !gem.ignoreByDefault().hasValue()
|
&& !gem.ignoreByDefault().hasValue()
|
||||||
&& !gem.builder().hasValue() ) {
|
&& !gem.builder().hasValue() ) {
|
||||||
|
|
||||||
@ -134,6 +137,15 @@ public class BeanMappingOptions extends DelegatingOptions {
|
|||||||
.orElse( next().getNullValueMappingStrategy() );
|
.orElse( next().getNullValueMappingStrategy() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ReportingPolicyGem unmappedTargetPolicy() {
|
||||||
|
return Optional.ofNullable( beanMapping ).map( BeanMappingGem::unmappedTargetPolicy )
|
||||||
|
.filter( GemValue::hasValue )
|
||||||
|
.map( GemValue::getValue )
|
||||||
|
.map( ReportingPolicyGem::valueOf )
|
||||||
|
.orElse( next().unmappedTargetPolicy() );
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BuilderGem getBuilder() {
|
public BuilderGem getBuilder() {
|
||||||
return Optional.ofNullable( beanMapping ).map( BeanMappingGem::builder )
|
return Optional.ofNullable( beanMapping ).map( BeanMappingGem::builder )
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* 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.unmappedtarget;
|
||||||
|
|
||||||
|
import org.mapstruct.BeanMapping;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.ReportingPolicy;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
@Mapper(unmappedTargetPolicy = ReportingPolicy.ERROR)
|
||||||
|
public interface BeanMappingSourceTargetMapper {
|
||||||
|
|
||||||
|
BeanMappingSourceTargetMapper INSTANCE = Mappers.getMapper( BeanMappingSourceTargetMapper.class );
|
||||||
|
|
||||||
|
@BeanMapping(unmappedTargetPolicy = ReportingPolicy.WARN)
|
||||||
|
Target sourceToTarget(Source source);
|
||||||
|
}
|
@ -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.unmappedtarget;
|
||||||
|
|
||||||
|
import org.mapstruct.BeanMapping;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.ReportingPolicy;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface ErroneousBeanMappingStrictSourceTargetMapper {
|
||||||
|
|
||||||
|
ErroneousBeanMappingStrictSourceTargetMapper INSTANCE =
|
||||||
|
Mappers.getMapper( ErroneousBeanMappingStrictSourceTargetMapper.class );
|
||||||
|
|
||||||
|
@BeanMapping(unmappedTargetPolicy = ReportingPolicy.ERROR)
|
||||||
|
Target sourceToTarget(Source source);
|
||||||
|
|
||||||
|
Source targetToSource(Target target);
|
||||||
|
}
|
@ -87,4 +87,38 @@ public class UnmappedProductTest {
|
|||||||
)
|
)
|
||||||
public void shouldRaiseErrorDueToUnsetTargetPropertyWithPolicySetViaProcessorOption() {
|
public void shouldRaiseErrorDueToUnsetTargetPropertyWithPolicySetViaProcessorOption() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
@IssueKey("2132")
|
||||||
|
@WithClasses({ Source.class, Target.class, ErroneousBeanMappingStrictSourceTargetMapper.class })
|
||||||
|
@ExpectedCompilationOutcome(
|
||||||
|
value = CompilationResult.FAILED,
|
||||||
|
diagnostics = {
|
||||||
|
@Diagnostic(type = ErroneousBeanMappingStrictSourceTargetMapper.class,
|
||||||
|
kind = Kind.ERROR,
|
||||||
|
line = 20,
|
||||||
|
message = "Unmapped target property: \"bar\"."),
|
||||||
|
@Diagnostic(type = ErroneousBeanMappingStrictSourceTargetMapper.class,
|
||||||
|
kind = Kind.WARNING,
|
||||||
|
line = 22,
|
||||||
|
message = "Unmapped target property: \"qux\".")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public void shouldRaiseErrorDueToUnsetTargetPropertyWithPolicySetViaBeanMapping() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
@IssueKey("2132")
|
||||||
|
@WithClasses({ Source.class, Target.class, BeanMappingSourceTargetMapper.class })
|
||||||
|
@ExpectedCompilationOutcome(
|
||||||
|
value = CompilationResult.SUCCEEDED,
|
||||||
|
diagnostics = {
|
||||||
|
@Diagnostic(type = BeanMappingSourceTargetMapper.class,
|
||||||
|
kind = Kind.WARNING,
|
||||||
|
line = 19,
|
||||||
|
message = "Unmapped target property: \"bar\".")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public void shouldLeaveUnmappedTargetPropertyUnsetWithWarnPolicySetViaBeanMapping() {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user