Skip to content

Commit 81a88bd

Browse files
authored
mapstruct#2133 @BeanMapping#resultType should not be applied to forged methods (mapstruct#2134)
* mapstruct#2133 reproducer * mapstruct#2133 solution
1 parent 082704c commit 81a88bd

3 files changed

Lines changed: 102 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,10 @@ private void sortPropertyMappingsByDependencies() {
472472
}
473473

474474
private Type getReturnTypeToConstructFromSelectionParameters(SelectionParameters selectionParams) {
475-
if ( selectionParams != null && selectionParams.getResultType() != null ) {
475+
// resultType only applies to method that actually has @BeanMapping annotation, never to forged methods
476+
if ( !( method instanceof ForgedMethod )
477+
&& selectionParams != null
478+
&& selectionParams.getResultType() != null ) {
476479
return ctx.getTypeFactory().getType( selectionParams.getResultType() );
477480
}
478481
return null;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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._2133;
7+
8+
import org.mapstruct.BeanMapping;
9+
import org.mapstruct.Mapper;
10+
import org.mapstruct.factory.Mappers;
11+
12+
@Mapper
13+
public interface Issue2133Mapper {
14+
15+
Issue2133Mapper INSTANCE = Mappers.getMapper( Issue2133Mapper.class );
16+
17+
@BeanMapping(resultType = Target.class)
18+
AbstractTarget map(Source source);
19+
20+
class Source {
21+
22+
private EmbeddedDto embedded;
23+
24+
public EmbeddedDto getEmbedded() {
25+
return embedded;
26+
}
27+
28+
public void setEmbedded(EmbeddedDto embedded) {
29+
this.embedded = embedded;
30+
}
31+
}
32+
33+
class Target extends AbstractTarget {
34+
}
35+
36+
abstract class AbstractTarget {
37+
38+
private EmbeddedEntity embedded;
39+
40+
public EmbeddedEntity getEmbedded() {
41+
return embedded;
42+
}
43+
44+
public void setEmbedded(EmbeddedEntity embedded) {
45+
this.embedded = embedded;
46+
}
47+
}
48+
49+
class EmbeddedDto {
50+
51+
private String s1;
52+
53+
public String getS1() {
54+
return s1;
55+
}
56+
57+
public void setS1(String s1) {
58+
this.s1 = s1;
59+
}
60+
}
61+
62+
class EmbeddedEntity {
63+
64+
private String s1;
65+
66+
public String getS1() {
67+
return s1;
68+
}
69+
70+
public void setS1(String s1) {
71+
this.s1 = s1;
72+
}
73+
74+
}
75+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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._2133;
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+
@IssueKey("2133")
15+
@WithClasses( Issue2133Mapper.class )
16+
@RunWith(AnnotationProcessorTestRunner.class)
17+
public class Issue2133Test {
18+
19+
@Test
20+
public void shouldCompile() {
21+
}
22+
23+
}

0 commit comments

Comments
 (0)