Skip to content

Commit feec705

Browse files
authored
Fix wrong unmapped target warning with Mapping annotation (#123)
1 parent b272649 commit feec705

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

src/main/java/org/mapstruct/intellij/inspection/UnmappedTargetPropertiesInspection.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public void visitMethod(PsiMethod method) {
8686

8787
// find and remove all defined mapping targets
8888
Set<String> definedTargets = TargetUtils.findAllDefinedMappingTargets( method, mapStructVersion )
89-
.collect( Collectors.toSet() );
89+
.map( MyJavaElementVisitor::getBaseTarget )
90+
.collect( Collectors.toSet() );
9091
allTargetProperties.removeAll( definedTargets );
9192

9293
if ( definedTargets.contains( "." ) ) {
@@ -141,6 +142,15 @@ public void visitMethod(PsiMethod method) {
141142
}
142143
}
143144

145+
@NotNull
146+
private static String getBaseTarget(@NotNull String target) {
147+
int dotIndex = target.indexOf( "." );
148+
if ( dotIndex > 0 ) {
149+
return target.substring( 0, dotIndex );
150+
}
151+
return target;
152+
}
153+
144154
private static boolean isBeanMappingIgnoreByDefault(PsiMethod method) {
145155
PsiAnnotation beanMapping = findAnnotation( method, true, MapstructUtil.BEAN_MAPPING_FQN );
146156
if ( beanMapping != null ) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.intellij.inspection;
7+
8+
import com.intellij.codeInsight.intention.IntentionAction;
9+
import org.jetbrains.annotations.NotNull;
10+
11+
import java.util.List;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
/**
16+
* @author hduelme
17+
*/
18+
public class UnmappedTargetPropertiesSubObjectMappingTest extends BaseInspectionTest {
19+
20+
@NotNull
21+
@Override
22+
protected Class<UnmappedTargetPropertiesInspection> getInspection() {
23+
return UnmappedTargetPropertiesInspection.class;
24+
}
25+
26+
@Override
27+
protected void setUp() throws Exception {
28+
super.setUp();
29+
myFixture.copyFileToProject(
30+
"UnmappedTargetPropertiesData.java",
31+
"org/example/data/UnmappedTargetPropertiesData.java"
32+
);
33+
}
34+
35+
public void testUnmappedTargetPropertiesSubObjectMapping() {
36+
doTest();
37+
List<IntentionAction> allQuickFixes = myFixture.getAllQuickFixes();
38+
39+
assertThat( allQuickFixes )
40+
.isEmpty();
41+
}
42+
}

testData/inspection/UnmappedTargetPropertiesData.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,16 @@ public void setMoreTarget(String moreTarget) {
7979
}
8080
}
8181

82+
public static class TargetWithInnerObject {
83+
private Target testTarget;
84+
85+
public Target getTestTarget() {
86+
return testTarget;
87+
}
88+
89+
public void setTestTarget(Target testTarget) {
90+
this.testTarget = testTarget;
91+
}
92+
}
93+
8294
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
7+
import org.mapstruct.Context;
8+
import org.mapstruct.Mapper;
9+
import org.mapstruct.Mapping;
10+
import org.mapstruct.MappingTarget;
11+
import org.mapstruct.Mappings;
12+
import org.example.data.UnmappedTargetPropertiesData.TargetWithInnerObject;
13+
import org.example.data.UnmappedTargetPropertiesData.Source;
14+
15+
@Mapper
16+
interface SingleMappingsMapper {
17+
18+
@Mappings({
19+
@Mapping(target = "testTarget.moreTarget", source = "moreSource")
20+
})
21+
TargetWithInnerObject map(Source source);
22+
}
23+
24+
@Mapper
25+
interface SingleMappingMapper {
26+
27+
@Mapping(target = "testTarget.testName", source = "name")
28+
TargetWithInnerObject map(Source source);
29+
}
30+
31+
@Mapper
32+
interface UpdateMapper {
33+
34+
@Mapping(target = "testTarget.moreTarget", source = "moreSource")
35+
void update(@MappingTarget TargetWithInnerObject target, Source source);
36+
}

0 commit comments

Comments
 (0)