mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2350 Generate core string to enum mapping when AnyRemaining or AnyUnmapped is not used
This commit is contained in:
parent
07f5189a72
commit
85af901ea7
@ -262,8 +262,8 @@ public class ValueMappingMethod extends MappingMethod {
|
|||||||
List<MappingEntry> mappings = new ArrayList<>();
|
List<MappingEntry> mappings = new ArrayList<>();
|
||||||
List<String> unmappedSourceConstants = new ArrayList<>( targetType.getEnumConstants() );
|
List<String> unmappedSourceConstants = new ArrayList<>( targetType.getEnumConstants() );
|
||||||
boolean sourceErrorOccurred = !reportErrorIfMappedTargetEnumConstantsDontExist( method, targetType );
|
boolean sourceErrorOccurred = !reportErrorIfMappedTargetEnumConstantsDontExist( method, targetType );
|
||||||
boolean mandatoryMissing = !reportErrorIfAnyRemainingOrAnyUnMappedMissing( method );
|
reportWarningIfAnyRemainingOrAnyUnMappedMissing( method );
|
||||||
if ( sourceErrorOccurred || mandatoryMissing ) {
|
if ( sourceErrorOccurred ) {
|
||||||
return mappings;
|
return mappings;
|
||||||
}
|
}
|
||||||
Set<String> mappedSources = new LinkedHashSet<>();
|
Set<String> mappedSources = new LinkedHashSet<>();
|
||||||
@ -344,17 +344,14 @@ public class ValueMappingMethod extends MappingMethod {
|
|||||||
return !foundIncorrectMapping;
|
return !foundIncorrectMapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean reportErrorIfAnyRemainingOrAnyUnMappedMissing(Method method) {
|
private void reportWarningIfAnyRemainingOrAnyUnMappedMissing(Method method) {
|
||||||
boolean foundIncorrectMapping = false;
|
|
||||||
|
|
||||||
if ( !( valueMappings.hasMapAnyUnmapped || valueMappings.hasMapAnyRemaining ) ) {
|
if ( !( valueMappings.hasMapAnyUnmapped || valueMappings.hasMapAnyRemaining ) ) {
|
||||||
ctx.getMessager().printMessage(
|
ctx.getMessager().printMessage(
|
||||||
method.getExecutable(),
|
method.getExecutable(),
|
||||||
Message.VALUEMAPPING_ANY_REMAINING_OR_UNMAPPED_MISSING
|
Message.VALUEMAPPING_ANY_REMAINING_OR_UNMAPPED_MISSING
|
||||||
);
|
);
|
||||||
foundIncorrectMapping = true;
|
|
||||||
}
|
}
|
||||||
return !foundIncorrectMapping;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean reportErrorIfMappedTargetEnumConstantsDontExist(Method method, Type targetType) {
|
private boolean reportErrorIfMappedTargetEnumConstantsDontExist(Method method, Type targetType) {
|
||||||
|
@ -16,6 +16,7 @@ import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutco
|
|||||||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
@IssueKey( "1557" )
|
@IssueKey( "1557" )
|
||||||
@WithClasses({ OrderType.class, OrderMapper.class })
|
@WithClasses({ OrderType.class, OrderMapper.class })
|
||||||
@ -53,7 +54,20 @@ public class StringToEnumMappingTest {
|
|||||||
"type String to an enum type." )
|
"type String to an enum type." )
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
public void shouldRaiseErrorWhenUsingAnyRemaining() {
|
public void shouldRaiseWarningWhenNotUsingAnyRemainingOrAnyUnmapped() {
|
||||||
|
|
||||||
|
assertThatThrownBy( () -> ErroneousOrderMapperUsingNoAnyRemainingAndNoAnyUnmapped.INSTANCE.map( "unknown" ) )
|
||||||
|
.isInstanceOf( IllegalArgumentException.class )
|
||||||
|
.hasMessage( "Unexpected enum constant: unknown" );
|
||||||
|
|
||||||
|
assertThat( ErroneousOrderMapperUsingNoAnyRemainingAndNoAnyUnmapped.INSTANCE.map( null ) )
|
||||||
|
.isEqualTo( OrderType.STANDARD );
|
||||||
|
|
||||||
|
assertThat( ErroneousOrderMapperUsingNoAnyRemainingAndNoAnyUnmapped.INSTANCE.map( "STANDARD" ) )
|
||||||
|
.isNull();
|
||||||
|
|
||||||
|
assertThat( ErroneousOrderMapperUsingNoAnyRemainingAndNoAnyUnmapped.INSTANCE.map( "RETAIL" ) )
|
||||||
|
.isEqualTo( OrderType.RETAIL );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user