-
-
Notifications
You must be signed in to change notification settings - Fork 1k
String -> Enum mapping own default behavior #3831
Copy link
Copy link
Open
Labels
Description
Use case
In String -> Enum mappings the default will be an exception throw, which is fine. With <ANY_UNMAPPED> or <ANY_REMAINING> we can override the exception with a "default" value. In my case I mapped it to Null, but it would be nice to get an log message like: "Could not found mapping for {stringValue}, returning default value {defaultValue}". In this way we are getting an information over our monitoring tool. Maybe something like this:
@ValueMapping(target = MappingConstants.NULL, source = MappingConstants.ANY_UNMAPPED, loggingMethod = "unknownMappingWarning")
Or an config, I am not sure what is the best way to do it.
Generated Code
// GENERATED CODE
public class SpecialOrderMapperImpl implements SpecialOrderMapper {
@Override
public ExternalOrderType orderTypeToExternalOrderType(String stringValue) {
if (stringValue == null) {
return ExternalOrderType.DEFAULT;
}
ExternalOrderType externalOrderType_;
switch ( stringValue ) {
case "STANDARD": externalOrderType_ = null;
break;
case "RETAIL": externalOrderType_ = ExternalOrderType.RETAIL;
break;
case "B2B": externalOrderType_ = ExternalOrderType.B2B;
break;
default:
logger.warn("Could not found mapping for {}, returning default value {}", stringValue, ExternalOrderType.DEFAULT);
externalOrderType_ = ExternalOrderType.SPECIAL;
}
return externalOrderType_;
}
}Possible workarounds
You could do it manually. With the qualifiedByName propertie, but you need to use the "normal" mapping annotation: @Mapping(...)
MapStruct Version
1.6.3
Reactions are currently unavailable