mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#1317 Add support for ignoring unmapped source properties
This property has only effect on the unmapped source properties report
This commit is contained in:
parent
35f5400e00
commit
7e7fcfbb94
@ -83,4 +83,18 @@ public @interface BeanMapping {
|
|||||||
* @since 1.3
|
* @since 1.3
|
||||||
*/
|
*/
|
||||||
boolean ignoreByDefault() default false;
|
boolean ignoreByDefault() default false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unmapped source properties to be ignored. This could be used when {@link ReportingPolicy#WARN}
|
||||||
|
* or {@link ReportingPolicy#ERROR} is used for {@link Mapper#unmappedSourcePolicy()} or
|
||||||
|
* {@link MapperConfig#unmappedSourcePolicy()}. Listed properties will be ignored when composing the unmapped
|
||||||
|
* source properties report.
|
||||||
|
* <p>
|
||||||
|
* <b>NOTE</b>: This does not support ignoring nested source properties
|
||||||
|
*
|
||||||
|
* @return The source properties that should be ignored when performing a report
|
||||||
|
*
|
||||||
|
* @since 1.3
|
||||||
|
*/
|
||||||
|
String[] ignoreUnmappedSourceProperties() default {};
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,7 @@ import org.mapstruct.ap.internal.model.common.Parameter;
|
|||||||
import org.mapstruct.ap.internal.model.common.Type;
|
import org.mapstruct.ap.internal.model.common.Type;
|
||||||
import org.mapstruct.ap.internal.model.dependency.GraphAnalyzer;
|
import org.mapstruct.ap.internal.model.dependency.GraphAnalyzer;
|
||||||
import org.mapstruct.ap.internal.model.dependency.GraphAnalyzer.GraphAnalyzerBuilder;
|
import org.mapstruct.ap.internal.model.dependency.GraphAnalyzer.GraphAnalyzerBuilder;
|
||||||
|
import org.mapstruct.ap.internal.model.source.BeanMapping;
|
||||||
import org.mapstruct.ap.internal.model.source.ForgedMethod;
|
import org.mapstruct.ap.internal.model.source.ForgedMethod;
|
||||||
import org.mapstruct.ap.internal.model.source.ForgedMethodHistory;
|
import org.mapstruct.ap.internal.model.source.ForgedMethodHistory;
|
||||||
import org.mapstruct.ap.internal.model.source.Mapping;
|
import org.mapstruct.ap.internal.model.source.Mapping;
|
||||||
@ -133,6 +134,14 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
existingVariableNames.addAll( method.getParameterNames() );
|
existingVariableNames.addAll( method.getParameterNames() );
|
||||||
|
|
||||||
|
BeanMapping beanMapping = method.getMappingOptions().getBeanMapping();
|
||||||
|
if ( beanMapping != null ) {
|
||||||
|
for ( String ignoreUnmapped : beanMapping.getIgnoreUnmappedSourceProperties() ) {
|
||||||
|
unprocessedSourceProperties.remove( ignoreUnmapped );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.internal.model.source;
|
package org.mapstruct.ap.internal.model.source;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
import javax.lang.model.element.ExecutableElement;
|
import javax.lang.model.element.ExecutableElement;
|
||||||
import javax.lang.model.type.TypeKind;
|
import javax.lang.model.type.TypeKind;
|
||||||
import javax.lang.model.util.Types;
|
import javax.lang.model.util.Types;
|
||||||
@ -39,6 +41,7 @@ public class BeanMapping {
|
|||||||
private final NullValueMappingStrategyPrism nullValueMappingStrategy;
|
private final NullValueMappingStrategyPrism nullValueMappingStrategy;
|
||||||
private final ReportingPolicyPrism reportingPolicy;
|
private final ReportingPolicyPrism reportingPolicy;
|
||||||
private final boolean ignoreByDefault;
|
private final boolean ignoreByDefault;
|
||||||
|
private final List<String> ignoreUnmappedSourceProperties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates a mapping for inheritance. Will set ignoreByDefault to false.
|
* creates a mapping for inheritance. Will set ignoreByDefault to false.
|
||||||
@ -47,7 +50,13 @@ public class BeanMapping {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static BeanMapping forInheritance( BeanMapping map ) {
|
public static BeanMapping forInheritance( BeanMapping map ) {
|
||||||
return new BeanMapping( map.selectionParameters, map.nullValueMappingStrategy, map.reportingPolicy, false );
|
return new BeanMapping(
|
||||||
|
map.selectionParameters,
|
||||||
|
map.nullValueMappingStrategy,
|
||||||
|
map.reportingPolicy,
|
||||||
|
false,
|
||||||
|
map.ignoreUnmappedSourceProperties
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BeanMapping fromPrism(BeanMappingPrism beanMapping, ExecutableElement method,
|
public static BeanMapping fromPrism(BeanMappingPrism beanMapping, ExecutableElement method,
|
||||||
@ -66,6 +75,7 @@ public class BeanMapping {
|
|||||||
|
|
||||||
boolean ignoreByDefault = beanMapping.ignoreByDefault();
|
boolean ignoreByDefault = beanMapping.ignoreByDefault();
|
||||||
if ( !resultTypeIsDefined && beanMapping.qualifiedBy().isEmpty() && beanMapping.qualifiedByName().isEmpty()
|
if ( !resultTypeIsDefined && beanMapping.qualifiedBy().isEmpty() && beanMapping.qualifiedByName().isEmpty()
|
||||||
|
&& beanMapping.ignoreUnmappedSourceProperties().isEmpty()
|
||||||
&& ( nullValueMappingStrategy == null ) && !ignoreByDefault ) {
|
&& ( nullValueMappingStrategy == null ) && !ignoreByDefault ) {
|
||||||
|
|
||||||
messager.printMessage( method, Message.BEANMAPPING_NO_ELEMENTS );
|
messager.printMessage( method, Message.BEANMAPPING_NO_ELEMENTS );
|
||||||
@ -79,7 +89,13 @@ public class BeanMapping {
|
|||||||
);
|
);
|
||||||
|
|
||||||
//TODO Do we want to add the reporting policy to the BeanMapping as well? To give more granular support?
|
//TODO Do we want to add the reporting policy to the BeanMapping as well? To give more granular support?
|
||||||
return new BeanMapping( cmp, nullValueMappingStrategy, null, ignoreByDefault );
|
return new BeanMapping(
|
||||||
|
cmp,
|
||||||
|
nullValueMappingStrategy,
|
||||||
|
null,
|
||||||
|
ignoreByDefault,
|
||||||
|
beanMapping.ignoreUnmappedSourceProperties()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -89,15 +105,17 @@ public class BeanMapping {
|
|||||||
* @return bean mapping that needs to be used for Mappings
|
* @return bean mapping that needs to be used for Mappings
|
||||||
*/
|
*/
|
||||||
public static BeanMapping forForgedMethods() {
|
public static BeanMapping forForgedMethods() {
|
||||||
return new BeanMapping( null, null, ReportingPolicyPrism.IGNORE, false );
|
return new BeanMapping( null, null, ReportingPolicyPrism.IGNORE, false, Collections.<String>emptyList() );
|
||||||
}
|
}
|
||||||
|
|
||||||
private BeanMapping(SelectionParameters selectionParameters, NullValueMappingStrategyPrism nvms,
|
private BeanMapping(SelectionParameters selectionParameters, NullValueMappingStrategyPrism nvms,
|
||||||
ReportingPolicyPrism reportingPolicy, boolean ignoreByDefault) {
|
ReportingPolicyPrism reportingPolicy, boolean ignoreByDefault,
|
||||||
|
List<String> ignoreUnmappedSourceProperties) {
|
||||||
this.selectionParameters = selectionParameters;
|
this.selectionParameters = selectionParameters;
|
||||||
this.nullValueMappingStrategy = nvms;
|
this.nullValueMappingStrategy = nvms;
|
||||||
this.reportingPolicy = reportingPolicy;
|
this.reportingPolicy = reportingPolicy;
|
||||||
this.ignoreByDefault = ignoreByDefault;
|
this.ignoreByDefault = ignoreByDefault;
|
||||||
|
this.ignoreUnmappedSourceProperties = ignoreUnmappedSourceProperties;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SelectionParameters getSelectionParameters() {
|
public SelectionParameters getSelectionParameters() {
|
||||||
@ -116,4 +134,7 @@ public class BeanMapping {
|
|||||||
return ignoreByDefault;
|
return ignoreByDefault;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> getIgnoreUnmappedSourceProperties() {
|
||||||
|
return ignoreUnmappedSourceProperties;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,41 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
* and/or other contributors as indicated by the @authors tag. See the
|
||||||
|
* copyright.txt file in the distribution for a full listing of all
|
||||||
|
* contributors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.source.ignore;
|
||||||
|
|
||||||
|
import org.mapstruct.BeanMapping;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.ReportingPolicy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@Mapper(
|
||||||
|
unmappedTargetPolicy = ReportingPolicy.IGNORE,
|
||||||
|
unmappedSourcePolicy = ReportingPolicy.ERROR
|
||||||
|
)
|
||||||
|
public interface IgnoreUnmappedSourceMapper {
|
||||||
|
|
||||||
|
@BeanMapping(
|
||||||
|
ignoreUnmappedSourceProperties = {
|
||||||
|
"name",
|
||||||
|
"surname"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
PersonDto map(Person person);
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
* and/or other contributors as indicated by the @authors tag. See the
|
||||||
|
* copyright.txt file in the distribution for a full listing of all
|
||||||
|
* contributors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.source.ignore;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@WithClasses({
|
||||||
|
IgnoreUnmappedSourceMapper.class,
|
||||||
|
Person.class,
|
||||||
|
PersonDto.class
|
||||||
|
})
|
||||||
|
@RunWith( AnnotationProcessorTestRunner.class )
|
||||||
|
@IssueKey("1317")
|
||||||
|
public class IgnoreUnmappedSourcePropertiesTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotReportErrorOnIgnoredUnmappedSourceProperties() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
* and/or other contributors as indicated by the @authors tag. See the
|
||||||
|
* copyright.txt file in the distribution for a full listing of all
|
||||||
|
* contributors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.source.ignore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
public class Person {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private String surname;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSurname() {
|
||||||
|
return surname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSurname(String surname) {
|
||||||
|
this.surname = surname;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
* and/or other contributors as indicated by the @authors tag. See the
|
||||||
|
* copyright.txt file in the distribution for a full listing of all
|
||||||
|
* contributors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.source.ignore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
public class PersonDto {
|
||||||
|
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user