mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2781 Remove unmapped source properties when source parameter is directly mapped
This commit is contained in:
parent
4843123e6e
commit
f3dac94701
@ -1295,8 +1295,16 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
||||
.options( mapping )
|
||||
.build();
|
||||
handledTargets.add( targetPropertyName );
|
||||
unprocessedSourceParameters.remove( sourceRef.getParameter() );
|
||||
unprocessedSourceProperties.remove( sourceRef.getShallowestPropertyName() );
|
||||
Parameter sourceParameter = sourceRef.getParameter();
|
||||
unprocessedSourceParameters.remove( sourceParameter );
|
||||
// If the source parameter was directly mapped
|
||||
if ( sourceRef.getPropertyEntries().isEmpty() ) {
|
||||
// Ignore all of its source properties completely
|
||||
ignoreSourceProperties( sourceParameter );
|
||||
}
|
||||
else {
|
||||
unprocessedSourceProperties.remove( sourceRef.getShallowestPropertyName() );
|
||||
}
|
||||
}
|
||||
else {
|
||||
errorOccured = true;
|
||||
@ -1471,23 +1479,25 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
||||
sourceParameters.remove();
|
||||
unprocessedDefinedTargets.remove( targetProperty.getKey() );
|
||||
unprocessedSourceProperties.remove( targetProperty.getKey() );
|
||||
|
||||
// The source parameter was directly mapped so ignore all of its source properties completely
|
||||
if ( !sourceParameter.getType().isPrimitive() && !sourceParameter.getType().isArrayType() ) {
|
||||
// We explicitly ignore source properties from primitives or array types
|
||||
Map<String, ReadAccessor> readAccessors = sourceParameter.getType()
|
||||
.getPropertyReadAccessors();
|
||||
for ( String sourceProperty : readAccessors.keySet() ) {
|
||||
unprocessedSourceProperties.remove( sourceProperty );
|
||||
}
|
||||
}
|
||||
|
||||
unprocessedConstructorProperties.remove( targetProperty.getKey() );
|
||||
ignoreSourceProperties( sourceParameter );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ignoreSourceProperties(Parameter sourceParameter) {
|
||||
// The source parameter was directly mapped so ignore all of its source properties completely
|
||||
if ( !sourceParameter.getType().isPrimitive() && !sourceParameter.getType().isArrayType() ) {
|
||||
// We explicitly ignore source properties from primitives or array types
|
||||
Map<String, ReadAccessor> readAccessors = sourceParameter.getType()
|
||||
.getPropertyReadAccessors();
|
||||
for ( String sourceProperty : readAccessors.keySet() ) {
|
||||
unprocessedSourceProperties.remove( sourceProperty );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private SourceReference getSourceRefByTargetName(Parameter sourceParameter, String targetPropertyName) {
|
||||
|
||||
SourceReference sourceRef = null;
|
||||
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.bugs._2781;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
* @author Mengxing Yuan
|
||||
*/
|
||||
@Mapper(unmappedSourcePolicy = ReportingPolicy.ERROR)
|
||||
public interface Issue2781Mapper {
|
||||
|
||||
Issue2781Mapper INSTANCE = Mappers.getMapper( Issue2781Mapper.class );
|
||||
|
||||
@Mapping(target = "nested", source = "source")
|
||||
Target map(Source source);
|
||||
|
||||
class Target {
|
||||
private Source nested;
|
||||
|
||||
public Source getNested() {
|
||||
return nested;
|
||||
}
|
||||
|
||||
public void setNested(Source nested) {
|
||||
this.nested = nested;
|
||||
}
|
||||
}
|
||||
|
||||
class Source {
|
||||
private String field1;
|
||||
private String field2;
|
||||
|
||||
public Source(String field1, String field2) {
|
||||
this.field1 = field1;
|
||||
this.field2 = field2;
|
||||
}
|
||||
|
||||
public String getField1() {
|
||||
return field1;
|
||||
}
|
||||
|
||||
public String getField2() {
|
||||
return field2;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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.bugs._2781;
|
||||
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
import org.mapstruct.ap.testutil.ProcessorTest;
|
||||
import org.mapstruct.ap.testutil.WithClasses;
|
||||
|
||||
/**
|
||||
* @author Mengxing Yuan
|
||||
*/
|
||||
@IssueKey("2781")
|
||||
@WithClasses({
|
||||
Issue2781Mapper.class
|
||||
})
|
||||
class Issue2781Test {
|
||||
|
||||
@ProcessorTest
|
||||
void shouldCompileWithoutErrors() {
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user