Skip to content

Commit 638e132

Browse files
authored
* mapstruct#2139 reproducer * mapstruct#2139 solution * mapstruct#2139 license
1 parent 12ac348 commit 638e132

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

processor/src/main/java/org/mapstruct/ap/internal/model/source/MappingMethodOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,10 @@ private boolean isRedefined(Set<String> redefinedNames, String inheritedName ) {
238238
}
239239

240240
private boolean elementsAreContainedIn( String redefinedName, String inheritedName ) {
241-
if ( redefinedName.startsWith( inheritedName ) ) {
241+
if ( inheritedName != null && redefinedName.startsWith( inheritedName ) ) {
242242
// it is possible to redefine an exact matching source name, because the same source can be mapped to
243243
// multiple targets. It is not possible for target, but caught by the Set and equals methoded in
244-
// MappingOptions
244+
// MappingOptions. SourceName == null also could hint at redefinition
245245
if ( redefinedName.length() > inheritedName.length() ) {
246246
// redefined.lenght() > inherited.length(), first following character should be separator
247247
return '.' == redefinedName.charAt( inheritedName.length() );

processor/src/test/java/org/mapstruct/ap/test/bugs/_2101/Issue2101Test.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,20 @@ public void shouldMapSomeAdditionalTests2() {
6464
assertThat( target.value3 ).isEqualTo( "test" );
6565

6666
}
67+
68+
@Test
69+
@WithClasses(Issue2102IgnoreAllButMapper.class)
70+
public void shouldApplyIgnoreAllButTemplateOfMethod1() {
71+
72+
Issue2102IgnoreAllButMapper.Source source = new Issue2102IgnoreAllButMapper.Source();
73+
source.value1 = "value1";
74+
source.value2 = "value2";
75+
76+
Issue2102IgnoreAllButMapper.Target target = Issue2102IgnoreAllButMapper.INSTANCE.map1( source );
77+
assertThat( target.value1 ).isEqualTo( "value1" );
78+
79+
target = Issue2102IgnoreAllButMapper.INSTANCE.map2( source );
80+
assertThat( target.value1 ).isEqualTo( "value2" );
81+
82+
}
6783
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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._2101;
7+
8+
import org.mapstruct.BeanMapping;
9+
import org.mapstruct.InheritConfiguration;
10+
import org.mapstruct.Mapper;
11+
import org.mapstruct.Mapping;
12+
import org.mapstruct.factory.Mappers;
13+
14+
@Mapper
15+
public interface Issue2102IgnoreAllButMapper {
16+
17+
Issue2102IgnoreAllButMapper INSTANCE = Mappers.getMapper( Issue2102IgnoreAllButMapper.class );
18+
19+
@BeanMapping( ignoreByDefault = true )
20+
@Mapping(target = "value1") // but do map value1
21+
Target map1(Source source);
22+
23+
@InheritConfiguration
24+
@Mapping(target = "value1", source = "value2" )
25+
Target map2(Source source);
26+
27+
//CHECKSTYLE:OFF
28+
class Source {
29+
public String value1;
30+
public String value2;
31+
}
32+
33+
class Target {
34+
public String value1;
35+
}
36+
//CHECKSTYLE:ON
37+
38+
}

0 commit comments

Comments
 (0)