Skip to content

Commit e8713fb

Browse files
committed
#2251 Fix incorrect code generated for constructor mapping from implicit source parameter matching
1 parent b90c0a9 commit e8713fb

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,7 @@ private void applyParameterNameBasedMapping() {
13151315
sourceParameters.remove();
13161316
unprocessedDefinedTargets.remove( targetProperty.getKey() );
13171317
unprocessedSourceProperties.remove( targetProperty.getKey() );
1318+
unprocessedConstructorProperties.remove( targetProperty.getKey() );
13181319
}
13191320
}
13201321
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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._2251;
7+
8+
import org.mapstruct.Mapper;
9+
import org.mapstruct.Mapping;
10+
import org.mapstruct.factory.Mappers;
11+
12+
@Mapper
13+
public interface Issue2251Mapper {
14+
15+
Issue2251Mapper INSTANCE = Mappers.getMapper( Issue2251Mapper.class );
16+
17+
@Mapping(target = "value1", source = "source.value")
18+
Target map(Source source, String value2);
19+
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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._2251;
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("2251")
20+
@RunWith(AnnotationProcessorTestRunner.class)
21+
@WithClasses({
22+
Issue2251Mapper.class,
23+
Source.class,
24+
Target.class,
25+
})
26+
public class Issue2251Test {
27+
28+
@Test
29+
public void shouldGenerateCorrectCode() {
30+
31+
Target target = Issue2251Mapper.INSTANCE.map( new Source( "source" ), "test" );
32+
33+
assertThat( target ).isNotNull();
34+
assertThat( target.getValue1() ).isEqualTo( "source" );
35+
assertThat( target.getValue2() ).isEqualTo( "test" );
36+
}
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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._2251;
7+
8+
/**
9+
* @author Filip Hrisafov
10+
*/
11+
public class Source {
12+
13+
private final String value;
14+
15+
public Source(String value) {
16+
this.value = value;
17+
}
18+
19+
public String getValue() {
20+
return value;
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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._2251;
7+
8+
public class Target {
9+
private final String value1;
10+
private final String value2;
11+
12+
public Target(String value1, String value2) {
13+
this.value1 = value1;
14+
this.value2 = value2;
15+
}
16+
17+
public String getValue1() {
18+
return value1;
19+
}
20+
21+
public String getValue2() {
22+
return value2;
23+
}
24+
}

0 commit comments

Comments
 (0)