mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2481 Report ignored source properties which are missing
This commit is contained in:
parent
845d83e9d5
commit
5d8fcfa033
@ -98,6 +98,7 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
|||||||
private Map<String, Accessor> unprocessedConstructorProperties;
|
private Map<String, Accessor> unprocessedConstructorProperties;
|
||||||
private Map<String, Accessor> unprocessedTargetProperties;
|
private Map<String, Accessor> unprocessedTargetProperties;
|
||||||
private Map<String, Accessor> unprocessedSourceProperties;
|
private Map<String, Accessor> unprocessedSourceProperties;
|
||||||
|
private Set<String> missingIgnoredSourceProperties;
|
||||||
private Set<String> targetProperties;
|
private Set<String> targetProperties;
|
||||||
private final List<PropertyMapping> propertyMappings = new ArrayList<>();
|
private final List<PropertyMapping> propertyMappings = new ArrayList<>();
|
||||||
private final Set<Parameter> unprocessedSourceParameters = new HashSet<>();
|
private final Set<Parameter> unprocessedSourceParameters = new HashSet<>();
|
||||||
@ -248,9 +249,12 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get bean mapping (when specified as annotation )
|
// get bean mapping (when specified as annotation )
|
||||||
|
this.missingIgnoredSourceProperties = new HashSet<>();
|
||||||
if ( beanMapping != null ) {
|
if ( beanMapping != null ) {
|
||||||
for ( String ignoreUnmapped : beanMapping.getIgnoreUnmappedSourceProperties() ) {
|
for ( String ignoreUnmapped : beanMapping.getIgnoreUnmappedSourceProperties() ) {
|
||||||
unprocessedSourceProperties.remove( ignoreUnmapped );
|
if ( unprocessedSourceProperties.remove( ignoreUnmapped ) == null ) {
|
||||||
|
missingIgnoredSourceProperties.add( ignoreUnmapped );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -283,6 +287,7 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
|||||||
// report errors on unmapped properties
|
// report errors on unmapped properties
|
||||||
reportErrorForUnmappedTargetPropertiesIfRequired();
|
reportErrorForUnmappedTargetPropertiesIfRequired();
|
||||||
reportErrorForUnmappedSourcePropertiesIfRequired();
|
reportErrorForUnmappedSourcePropertiesIfRequired();
|
||||||
|
reportErrorForMissingIgnoredSourceProperties();
|
||||||
|
|
||||||
// mapNullToDefault
|
// mapNullToDefault
|
||||||
boolean mapNullToDefault = method.getOptions()
|
boolean mapNullToDefault = method.getOptions()
|
||||||
@ -1511,6 +1516,24 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void reportErrorForMissingIgnoredSourceProperties() {
|
||||||
|
if ( !missingIgnoredSourceProperties.isEmpty() ) {
|
||||||
|
Object[] args = new Object[] {
|
||||||
|
MessageFormat.format(
|
||||||
|
"{0,choice,1#property|1<properties}: \"{1}\"",
|
||||||
|
missingIgnoredSourceProperties.size(),
|
||||||
|
Strings.join( missingIgnoredSourceProperties, ", " )
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.getMessager().printMessage(
|
||||||
|
method.getExecutable(),
|
||||||
|
Message.BEANMAPPING_MISSING_IGNORED_SOURCES_ERROR,
|
||||||
|
args
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ConstructorAccessor {
|
private static class ConstructorAccessor {
|
||||||
|
@ -85,7 +85,6 @@ public class MapperOptions extends DelegatingOptions {
|
|||||||
public ReportingPolicyGem unmappedTargetPolicy() {
|
public ReportingPolicyGem unmappedTargetPolicy() {
|
||||||
return mapper.unmappedTargetPolicy().hasValue() ?
|
return mapper.unmappedTargetPolicy().hasValue() ?
|
||||||
ReportingPolicyGem.valueOf( mapper.unmappedTargetPolicy().get() ) : next().unmappedTargetPolicy();
|
ReportingPolicyGem.valueOf( mapper.unmappedTargetPolicy().get() ) : next().unmappedTargetPolicy();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -37,6 +37,7 @@ public enum Message {
|
|||||||
BEANMAPPING_UNMAPPED_FORGED_TARGETS_ERROR( "Unmapped target %s. Mapping from %s to %s." ),
|
BEANMAPPING_UNMAPPED_FORGED_TARGETS_ERROR( "Unmapped target %s. Mapping from %s to %s." ),
|
||||||
BEANMAPPING_UNMAPPED_SOURCES_WARNING( "Unmapped source %s.", Diagnostic.Kind.WARNING ),
|
BEANMAPPING_UNMAPPED_SOURCES_WARNING( "Unmapped source %s.", Diagnostic.Kind.WARNING ),
|
||||||
BEANMAPPING_UNMAPPED_SOURCES_ERROR( "Unmapped source %s." ),
|
BEANMAPPING_UNMAPPED_SOURCES_ERROR( "Unmapped source %s." ),
|
||||||
|
BEANMAPPING_MISSING_IGNORED_SOURCES_ERROR( "Ignored unknown source %s." ),
|
||||||
BEANMAPPING_CYCLE_BETWEEN_PROPERTIES( "Cycle(s) between properties given via dependsOn(): %s." ),
|
BEANMAPPING_CYCLE_BETWEEN_PROPERTIES( "Cycle(s) between properties given via dependsOn(): %s." ),
|
||||||
BEANMAPPING_UNKNOWN_PROPERTY_IN_DEPENDS_ON( "\"%s\" is no property of the method return type." ),
|
BEANMAPPING_UNKNOWN_PROPERTY_IN_DEPENDS_ON( "\"%s\" is no property of the method return type." ),
|
||||||
BEANMAPPING_IGNORE_BY_DEFAULT_WITH_MAPPING_TARGET_THIS( "Using @BeanMapping( ignoreByDefault = true ) with @Mapping( target = \".\", ... ) is not allowed. You'll need to explicitly ignore the target properties that should be ignored instead." ),
|
BEANMAPPING_IGNORE_BY_DEFAULT_WITH_MAPPING_TARGET_THIS( "Using @BeanMapping( ignoreByDefault = true ) with @Mapping( target = \".\", ... ) is not allowed. You'll need to explicitly ignore the target properties that should be ignored instead." ),
|
||||||
|
@ -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.missingignoredsource;
|
||||||
|
|
||||||
|
import org.mapstruct.BeanMapping;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.ReportingPolicy;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
@Mapper(
|
||||||
|
unmappedTargetPolicy = ReportingPolicy.IGNORE,
|
||||||
|
unmappedSourcePolicy = ReportingPolicy.IGNORE)
|
||||||
|
public interface ErroneousSourceTargetMapper {
|
||||||
|
ErroneousSourceTargetMapper INSTANCE = Mappers.getMapper( ErroneousSourceTargetMapper.class );
|
||||||
|
|
||||||
|
@BeanMapping(ignoreUnmappedSourceProperties = "bar")
|
||||||
|
Object sourceToTarget(Object source);
|
||||||
|
}
|
@ -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.missingignoredsource;
|
||||||
|
|
||||||
|
import javax.tools.Diagnostic.Kind;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
public class MissingIgnoredSourceTest {
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
@WithClasses({ ErroneousSourceTargetMapper.class })
|
||||||
|
@ExpectedCompilationOutcome(
|
||||||
|
value = CompilationResult.FAILED,
|
||||||
|
diagnostics = {
|
||||||
|
@Diagnostic(type = ErroneousSourceTargetMapper.class,
|
||||||
|
kind = Kind.ERROR,
|
||||||
|
line = 20,
|
||||||
|
message = "Ignored unknown source property: \"bar\".")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public void shouldRaiseErrorDueToMissingIgnoredSourceProperty() {
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user