#3238 Compile error instead of null pointer exception for invalid ignore with target this

This commit is contained in:
Filip Hrisafov 2023-05-01 09:44:05 +02:00
parent d0e4c48228
commit a89c34f00c
4 changed files with 83 additions and 0 deletions

View File

@ -254,6 +254,9 @@ public class MappingOptions extends DelegatingOptions {
&& gem.ignore().hasValue() && gem.ignore().getValue() ) {
message = Message.PROPERTYMAPPING_IGNORE_AND_NVPMS;
}
else if ( ".".equals( gem.target().get() ) && gem.ignore().hasValue() && gem.ignore().getValue() ) {
message = Message.PROPERTYMAPPING_TARGET_THIS_AND_IGNORE;
}
if ( message == null ) {
return true;

View File

@ -68,6 +68,7 @@ public enum Message {
PROPERTYMAPPING_CONSTANT_VALUE_AND_NVPMS( "Constant and nullValuePropertyMappingStrategy are both defined in @Mapping, either define a constant or an nullValuePropertyMappingStrategy." ),
PROPERTYMAPPING_DEFAULT_EXPERSSION_AND_NVPMS( "DefaultExpression and nullValuePropertyMappingStrategy are both defined in @Mapping, either define a defaultExpression or an nullValuePropertyMappingStrategy." ),
PROPERTYMAPPING_IGNORE_AND_NVPMS( "Ignore and nullValuePropertyMappingStrategy are both defined in @Mapping, either define ignore or an nullValuePropertyMappingStrategy." ),
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." ),
PROPERTYMAPPING_EXPRESSION_AND_QUALIFIER_BOTH_DEFINED("Expression and a qualifier both defined in @Mapping, either define an expression or a qualifier."),
PROPERTYMAPPING_INVALID_EXPRESSION( "Value for expression must be given in the form \"java(<EXPRESSION>)\"." ),
PROPERTYMAPPING_INVALID_DEFAULT_EXPRESSION( "Value for default expression must be given in the form \"java(<EXPRESSION>)\"." ),

View File

@ -0,0 +1,42 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._3238;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@Mapper
public interface ErroneousIssue3238Mapper {
@Mapping(target = ".", ignore = true)
Target map(Source source);
class Target {
private final String value;
public Target(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
class Source {
private final String value;
public Source(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._3238;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
/**
* @author Filip Hrisafov
*/
@WithClasses(ErroneousIssue3238Mapper.class)
@IssueKey("3238")
class Issue3238Test {
@ProcessorTest
@ExpectedCompilationOutcome(
value = CompilationResult.FAILED,
diagnostics = @Diagnostic( type = ErroneousIssue3238Mapper.class,
kind = javax.tools.Diagnostic.Kind.ERROR,
line = 14,
message = "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."
)
)
void shouldGenerateValidCompileError() {
}
}