#2132 Add unmappedTargetPolicy to @BeanMapping

This commit is contained in:
Lukas Lazar 2021-04-14 14:39:22 +02:00 committed by Filip Hrisafov
parent 4576103752
commit cc1562c5ad
7 changed files with 104 additions and 0 deletions

View File

@ -140,6 +140,17 @@ public @interface BeanMapping {
*/
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
* for the builder strategy that one uses.

View File

@ -245,6 +245,7 @@ Supported values are:
* `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 bean mapping via `@BeanMapping#unmappedTargetPolicy()`, it takes precedence over both `@Mapper#unmappedTargetPolicy()` and the option.
|`WARN`
|===

View File

@ -1390,6 +1390,9 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
if ( mappingReferences.isForForgedMethods() ) {
return ReportingPolicyGem.IGNORE;
}
if ( method.getOptions().getBeanMapping() != null ) {
return method.getOptions().getBeanMapping().unmappedTargetPolicy();
}
return method.getOptions().getMapper().unmappedTargetPolicy();
}

View File

@ -11,6 +11,8 @@ import java.util.Objects;
import java.util.Optional;
import javax.lang.model.element.AnnotationMirror;
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.TypeUtils;
@ -88,6 +90,7 @@ public class BeanMappingOptions extends DelegatingOptions {
&& !gem.nullValueCheckStrategy().hasValue()
&& !gem.nullValuePropertyMappingStrategy().hasValue()
&& !gem.nullValueMappingStrategy().hasValue()
&& !gem.unmappedTargetPolicy().hasValue()
&& !gem.ignoreByDefault().hasValue()
&& !gem.builder().hasValue() ) {
@ -134,6 +137,15 @@ public class BeanMappingOptions extends DelegatingOptions {
.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
public BuilderGem getBuilder() {
return Optional.ofNullable( beanMapping ).map( BeanMappingGem::builder )

View File

@ -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);
}

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.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);
}

View File

@ -87,4 +87,38 @@ public class UnmappedProductTest {
)
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() {
}
}