Skip to content

Commit 700293f

Browse files
committed
mapstruct#2301 Implicitly ignore forward inherited mappings from different method types
1 parent f84f756 commit 700293f

5 files changed

Lines changed: 165 additions & 0 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,15 @@ private boolean handleDefinedMapping(MappingReference mappingRef, Type resultTyp
938938

939939
if ( targetWriteAccessor == null ) {
940940
if ( targetReadAccessor == null ) {
941+
if ( mapping.getInheritContext() != null && mapping.getInheritContext().isForwarded() &&
942+
mapping.getInheritContext().getTemplateMethod().isUpdateMethod() != method.isUpdateMethod() ) {
943+
// When a configuration is inherited and the template method is not same type as the current
944+
// method then we can safely ignore this mapping.
945+
// This means that a property which is inherited might be present for a direct mapping
946+
// via the Builder, but not for an update mapping (directly on the object itself),
947+
// or vice versa
948+
return false;
949+
}
941950
Set<String> readAccessors = resultTypeToMap.getPropertyReadAccessors().keySet();
942951
String mostSimilarProperty = Strings.getMostSimilarWord( targetPropertyName, readAccessors );
943952

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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._2301;
7+
8+
import java.util.Set;
9+
10+
/**
11+
* @author Filip Hrisafov
12+
*/
13+
public class Artifact {
14+
15+
private String name;
16+
private Set<String> dependantBuildRecords;
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
26+
public Set<String> getDependantBuildRecords() {
27+
return dependantBuildRecords;
28+
}
29+
30+
public void setDependantBuildRecords(Set<String> dependantBuildRecords) {
31+
this.dependantBuildRecords = dependantBuildRecords;
32+
}
33+
34+
public static Builder builder() {
35+
return new Builder();
36+
}
37+
38+
public static class Builder {
39+
40+
private String name;
41+
private Set<String> dependantBuildRecords;
42+
43+
public Artifact build() {
44+
Artifact artifact = new Artifact();
45+
artifact.setName( name );
46+
artifact.setDependantBuildRecords( dependantBuildRecords );
47+
48+
return artifact;
49+
}
50+
51+
public Builder name(String name) {
52+
this.name = name;
53+
return this;
54+
}
55+
56+
public Builder dependantBuildRecord(String dependantBuildRecord) {
57+
this.dependantBuildRecords.add( dependantBuildRecord );
58+
return this;
59+
}
60+
61+
public Builder dependantBuildRecords(Set<String> dependantBuildRecords) {
62+
this.dependantBuildRecords = dependantBuildRecords;
63+
return this;
64+
}
65+
}
66+
}
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._2301;
7+
8+
/**
9+
* @author Filip Hrisafov
10+
*/
11+
public class ArtifactDto {
12+
13+
private final String name;
14+
15+
public ArtifactDto(String name) {
16+
this.name = name;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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._2301;
7+
8+
import org.mapstruct.InheritConfiguration;
9+
import org.mapstruct.Mapper;
10+
import org.mapstruct.Mapping;
11+
import org.mapstruct.MappingTarget;
12+
import org.mapstruct.factory.Mappers;
13+
14+
/**
15+
* @author Filip Hrisafov
16+
*/
17+
@Mapper
18+
public interface Issue2301Mapper {
19+
20+
Issue2301Mapper INSTANCE = Mappers.getMapper( Issue2301Mapper.class );
21+
22+
@Mapping(target = "dependantBuildRecords", ignore = true)
23+
@Mapping(target = "dependantBuildRecord", ignore = true)
24+
Artifact map(ArtifactDto dto);
25+
26+
@InheritConfiguration
27+
void update(@MappingTarget Artifact artifact, ArtifactDto dto);
28+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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._2301;
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("2301")
20+
@RunWith(AnnotationProcessorTestRunner.class)
21+
@WithClasses({
22+
Artifact.class,
23+
ArtifactDto.class,
24+
Issue2301Mapper.class
25+
})
26+
public class Issue2301Test {
27+
28+
@Test
29+
public void shouldCorrectlyIgnoreProperties() {
30+
Artifact artifact = Issue2301Mapper.INSTANCE.map( new ArtifactDto( "mapstruct" ) );
31+
32+
assertThat( artifact ).isNotNull();
33+
assertThat( artifact.getName() ).isEqualTo( "mapstruct" );
34+
assertThat( artifact.getDependantBuildRecords() ).isNull();
35+
36+
Issue2301Mapper.INSTANCE.update( artifact, new ArtifactDto( "mapstruct-processor" ) );
37+
38+
assertThat( artifact.getName() ).isEqualTo( "mapstruct-processor" );
39+
}
40+
}

0 commit comments

Comments
 (0)