#3485 Exception for Mapping to target = "." without source

This commit is contained in:
hduelme 2024-03-03 18:59:32 +01:00 committed by GitHub
parent c374b5267f
commit 2c12e75bfc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 72 additions and 0 deletions

View File

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

View File

@ -71,6 +71,7 @@ public enum Message {
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_TARGET_THIS_NO_SOURCE( "Using @Mapping( target = \".\") requires a source property. Expression or constant cannot be used as a source."),
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,35 @@
/*
* 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._3485;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
/**
* @author hduelme
*/
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface ErroneousIssue3485Mapper {
ErroneousIssue3485Mapper INSTANCE = Mappers.getMapper( ErroneousIssue3485Mapper.class );
class Target {
private final String value;
public Target( String value ) {
this.value = value;
}
public String getValue() {
return value;
}
}
@Mapping(target = ".")
Target targetFromExpression(String s);
}

View File

@ -0,0 +1,33 @@
/*
* 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._3485;
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 hduelme
*/
@IssueKey("3463")
public class Issue3485Test {
@ProcessorTest
@WithClasses(ErroneousIssue3485Mapper.class)
@ExpectedCompilationOutcome(value = CompilationResult.FAILED,
diagnostics = {
@Diagnostic(type = ErroneousIssue3485Mapper.class,
kind = javax.tools.Diagnostic.Kind.ERROR,
line = 33,
message = "Using @Mapping( target = \".\") requires a source property. Expression or " +
"constant cannot be used as a source.")
})
void thisMappingWithoutSource() {
}
}