Skip to content

Commit 628ff17

Browse files
committed
#2253 remove unmapped source properties when source parameter is directly mapped
1 parent 934d096 commit 628ff17

3 files changed

Lines changed: 92 additions & 0 deletions

File tree

processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,16 @@ private void applyParameterNameBasedMapping() {
13151315
sourceParameters.remove();
13161316
unprocessedDefinedTargets.remove( targetProperty.getKey() );
13171317
unprocessedSourceProperties.remove( targetProperty.getKey() );
1318+
1319+
// The source parameter was directly mapped so ignore all of its source properties completely
1320+
if ( !sourceParameter.getType().isPrimitive() && !sourceParameter.getType().isArrayType() ) {
1321+
// We explicitly ignore source properties from primitives or array types
1322+
Map<String, Accessor> readAccessors = sourceParameter.getType().getPropertyReadAccessors();
1323+
for ( String sourceProperty : readAccessors.keySet() ) {
1324+
unprocessedSourceProperties.remove( sourceProperty );
1325+
}
1326+
}
1327+
13181328
unprocessedConstructorProperties.remove( targetProperty.getKey() );
13191329
}
13201330
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.ap.test.bugs._2253;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.mapstruct.ap.testutil.IssueKey;
11+
import org.mapstruct.ap.testutil.WithClasses;
12+
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
16+
/**
17+
* @author Filip Hrisafov
18+
*/
19+
@IssueKey("2253")
20+
@RunWith( AnnotationProcessorTestRunner.class )
21+
@WithClasses( {
22+
TestMapper.class,
23+
} )
24+
public class Issue2253Test {
25+
26+
@Test
27+
public void shouldNotTreatMatchedSourceParameterAsBean() {
28+
29+
TestMapper.Person person = TestMapper.INSTANCE.map( new TestMapper.PersonDto( 20 ), "Tester" );
30+
31+
assertThat( person.getAge() ).isEqualTo( 20 );
32+
assertThat( person.getName() ).isEqualTo( "Tester" );
33+
}
34+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.ap.test.bugs._2253;
7+
8+
import org.mapstruct.Mapper;
9+
import org.mapstruct.ReportingPolicy;
10+
import org.mapstruct.factory.Mappers;
11+
12+
@Mapper(unmappedSourcePolicy = ReportingPolicy.ERROR)
13+
public interface TestMapper {
14+
15+
TestMapper INSTANCE = Mappers.getMapper( TestMapper.class );
16+
17+
Person map(PersonDto personDto, String name);
18+
19+
class Person {
20+
private final int age;
21+
private final String name;
22+
23+
public Person(int age, String name) {
24+
this.age = age;
25+
this.name = name;
26+
}
27+
28+
public int getAge() {
29+
return age;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
}
36+
37+
class PersonDto {
38+
private final int age;
39+
40+
public PersonDto(int age) {
41+
this.age = age;
42+
}
43+
44+
public int getAge() {
45+
return age;
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)