#2253 remove unmapped source properties when source parameter is directly mapped

This commit is contained in:
Filip Hrisafov 2020-11-06 21:24:52 +01:00
parent 934d096fb4
commit 628ff175e0
3 changed files with 92 additions and 0 deletions

View File

@ -1315,6 +1315,16 @@ 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, Accessor> readAccessors = sourceParameter.getType().getPropertyReadAccessors();
for ( String sourceProperty : readAccessors.keySet() ) {
unprocessedSourceProperties.remove( sourceProperty );
}
}
unprocessedConstructorProperties.remove( targetProperty.getKey() );
}
}

View File

@ -0,0 +1,34 @@
/*
* 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._2253;
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;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Filip Hrisafov
*/
@IssueKey("2253")
@RunWith( AnnotationProcessorTestRunner.class )
@WithClasses( {
TestMapper.class,
} )
public class Issue2253Test {
@Test
public void shouldNotTreatMatchedSourceParameterAsBean() {
TestMapper.Person person = TestMapper.INSTANCE.map( new TestMapper.PersonDto( 20 ), "Tester" );
assertThat( person.getAge() ).isEqualTo( 20 );
assertThat( person.getName() ).isEqualTo( "Tester" );
}
}

View File

@ -0,0 +1,48 @@
/*
* 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._2253;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
@Mapper(unmappedSourcePolicy = ReportingPolicy.ERROR)
public interface TestMapper {
TestMapper INSTANCE = Mappers.getMapper( TestMapper.class );
Person map(PersonDto personDto, String name);
class Person {
private final int age;
private final String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
}
class PersonDto {
private final int age;
public PersonDto(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
}