Skip to content

Commit a89c34f

Browse files
committed
#3238 Compile error instead of null pointer exception for invalid ignore with target this
1 parent d0e4c48 commit a89c34f

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ else if ( gem.nullValuePropertyMappingStrategy().hasValue()
254254
&& gem.ignore().hasValue() && gem.ignore().getValue() ) {
255255
message = Message.PROPERTYMAPPING_IGNORE_AND_NVPMS;
256256
}
257+
else if ( ".".equals( gem.target().get() ) && gem.ignore().hasValue() && gem.ignore().getValue() ) {
258+
message = Message.PROPERTYMAPPING_TARGET_THIS_AND_IGNORE;
259+
}
257260

258261
if ( message == null ) {
259262
return true;

processor/src/main/java/org/mapstruct/ap/internal/util/Message.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public enum Message {
6868
PROPERTYMAPPING_CONSTANT_VALUE_AND_NVPMS( "Constant and nullValuePropertyMappingStrategy are both defined in @Mapping, either define a constant or an nullValuePropertyMappingStrategy." ),
6969
PROPERTYMAPPING_DEFAULT_EXPERSSION_AND_NVPMS( "DefaultExpression and nullValuePropertyMappingStrategy are both defined in @Mapping, either define a defaultExpression or an nullValuePropertyMappingStrategy." ),
7070
PROPERTYMAPPING_IGNORE_AND_NVPMS( "Ignore and nullValuePropertyMappingStrategy are both defined in @Mapping, either define ignore or an nullValuePropertyMappingStrategy." ),
71+
PROPERTYMAPPING_TARGET_THIS_AND_IGNORE( "Using @Mapping( target = \".\", ignore = true ) is not allowed. You need to use @BeanMapping( ignoreByDefault = true ) if you would like to ignore all non explicitly mapped target properties." ),
7172
PROPERTYMAPPING_EXPRESSION_AND_QUALIFIER_BOTH_DEFINED("Expression and a qualifier both defined in @Mapping, either define an expression or a qualifier."),
7273
PROPERTYMAPPING_INVALID_EXPRESSION( "Value for expression must be given in the form \"java(<EXPRESSION>)\"." ),
7374
PROPERTYMAPPING_INVALID_DEFAULT_EXPRESSION( "Value for default expression must be given in the form \"java(<EXPRESSION>)\"." ),
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.ap.test.bugs._3238;
7+
8+
import org.mapstruct.Mapper;
9+
import org.mapstruct.Mapping;
10+
11+
@Mapper
12+
public interface ErroneousIssue3238Mapper {
13+
14+
@Mapping(target = ".", ignore = true)
15+
Target map(Source source);
16+
17+
class Target {
18+
19+
private final String value;
20+
21+
public Target(String value) {
22+
this.value = value;
23+
}
24+
25+
public String getValue() {
26+
return value;
27+
}
28+
}
29+
30+
class Source {
31+
32+
private final String value;
33+
34+
public Source(String value) {
35+
this.value = value;
36+
}
37+
38+
public String getValue() {
39+
return value;
40+
}
41+
}
42+
}
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._3238;
7+
8+
import org.mapstruct.ap.testutil.IssueKey;
9+
import org.mapstruct.ap.testutil.ProcessorTest;
10+
import org.mapstruct.ap.testutil.WithClasses;
11+
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
12+
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
13+
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
14+
15+
/**
16+
* @author Filip Hrisafov
17+
*/
18+
@WithClasses(ErroneousIssue3238Mapper.class)
19+
@IssueKey("3238")
20+
class Issue3238Test {
21+
22+
@ProcessorTest
23+
@ExpectedCompilationOutcome(
24+
value = CompilationResult.FAILED,
25+
diagnostics = @Diagnostic( type = ErroneousIssue3238Mapper.class,
26+
kind = javax.tools.Diagnostic.Kind.ERROR,
27+
line = 14,
28+
message = "Using @Mapping( target = \".\", ignore = true ) is not allowed." +
29+
" You need to use @BeanMapping( ignoreByDefault = true ) if you would like to ignore" +
30+
" all non explicitly mapped target properties."
31+
)
32+
)
33+
void shouldGenerateValidCompileError() {
34+
35+
}
36+
37+
}

0 commit comments

Comments
 (0)