diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java b/src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java index 10c4b68f7c..13f850fa89 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java @@ -1100,7 +1100,8 @@ public JsonDeserializer> createEnumDeserializer(DeserializationContext ctxt, config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS), constructEnumNamingStrategyResolver(config, beanDesc.getClassInfo()), // since 2.16 - EnumResolver.constructUsingToString(config, beanDesc.getClassInfo()) + EnumResolver.constructUsingToString(config, beanDesc.getClassInfo()), + constructEnumResolverNumberShape(enumClass, config, beanDesc) ); } } @@ -1815,6 +1816,21 @@ protected EnumResolver constructEnumResolver(Class> enumClass, return EnumResolver.constructFor(config, beanDesc.getClassInfo()); } + + protected EnumResolver constructEnumResolverNumberShape(Class> enumClass, + DeserializationConfig config, BeanDescription beanDesc) + { + AnnotatedMember jvAcc = beanDesc.findJsonValueAccessor(); + if (jvAcc != null) { + if (config.canOverrideAccessModifiers()) { + ClassUtil.checkAndFixAccess(jvAcc.getMember(), + config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS)); + } + return EnumResolver.constructUsingNumberShape(config, beanDesc.getClassInfo(), jvAcc); + } + return null; + } + /** * Factory method used to resolve an instance of {@link CompactStringObjectMap} * with {@link EnumNamingStrategy} applied for the target class. diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java index 48c14f1e09..a3f91fbcdd 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java @@ -54,8 +54,22 @@ public class EnumDeserializer */ protected volatile CompactStringObjectMap _lookupByToString; + /** + * We may also have integer-type of representation for Enum's, along with `@JsonValue`. + * + * @since 2.20.0 + */ + protected final CompactStringObjectMap _lookupByShapeNumberInt; + protected final Boolean _caseInsensitive; + /** + * Flag to check if FormatShape of int number type would be used to deserialize + * + * @since 2.21.0 + */ + protected final boolean _isShapeNumberInt; + private Boolean _useDefaultValueForUnknownEnum; private Boolean _useNullForUnknownEnum; @@ -71,7 +85,7 @@ public class EnumDeserializer * Marker flag that indicates whether the Enum class has {@code @JsonValue} * annotated accessor (or equivalent), used to populate {@link #_lookupByName}. * - * @since 2.20 + * @since 2.21.0 */ protected final boolean _hasAsValueAnnotation; @@ -109,15 +123,28 @@ public EnumDeserializer(EnumResolver byNameResolver, boolean caseInsensitive, _enumDefaultValue = byNameResolver.getDefaultValue(); _caseInsensitive = caseInsensitive; _isFromIntValue = byNameResolver.isFromIntValue(); + _isShapeNumberInt = false; _lookupByEnumNaming = byEnumNamingResolver == null ? null : byEnumNamingResolver.constructLookup(); _lookupByToString = null; + _lookupByShapeNumberInt = null; } /** * @since 2.16 + * @deprecated since 2.21.0 */ + @Deprecated public EnumDeserializer(EnumResolver byNameResolver, boolean caseInsensitive, EnumResolver byEnumNamingResolver, EnumResolver toStringResolver) + { + this(byNameResolver, caseInsensitive, byEnumNamingResolver, toStringResolver, null); + } + + /** + * @since 2.21.0 + */ + public EnumDeserializer(EnumResolver byNameResolver, boolean caseInsensitive, + EnumResolver byEnumNamingResolver, EnumResolver toStringResolver, EnumResolver shapeNumberResolver) { super(byNameResolver.getEnumClass()); _lookupByName = byNameResolver.constructLookup(); @@ -126,8 +153,10 @@ public EnumDeserializer(EnumResolver byNameResolver, boolean caseInsensitive, _enumDefaultValue = byNameResolver.getDefaultValue(); _caseInsensitive = caseInsensitive; _isFromIntValue = byNameResolver.isFromIntValue(); + _isShapeNumberInt = shapeNumberResolver != null; _lookupByEnumNaming = byEnumNamingResolver == null ? null : byEnumNamingResolver.constructLookup(); _lookupByToString = toStringResolver == null ? null : toStringResolver.constructLookup(); + _lookupByShapeNumberInt = shapeNumberResolver == null ? null : shapeNumberResolver.constructLookup(); } /** @@ -143,10 +172,12 @@ protected EnumDeserializer(EnumDeserializer base, Boolean caseInsensitive, _enumDefaultValue = base._enumDefaultValue; _caseInsensitive = Boolean.TRUE.equals(caseInsensitive); _isFromIntValue = base._isFromIntValue; + _isShapeNumberInt = base._isShapeNumberInt; _useDefaultValueForUnknownEnum = useDefaultValueForUnknownEnum; _useNullForUnknownEnum = useNullForUnknownEnum; _lookupByEnumNaming = base._lookupByEnumNaming; _lookupByToString = base._lookupByToString; + _lookupByShapeNumberInt = base._lookupByShapeNumberInt; } /** @@ -238,7 +269,7 @@ public JsonDeserializer> createContextual(DeserializationContext ctxt, JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)).orElse(_useDefaultValueForUnknownEnum); Boolean useNullForUnknownEnum = Optional.ofNullable(findFormatFeature(ctxt, property, handledType(), JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)).orElse(_useNullForUnknownEnum); - + return withResolved(caseInsensitive, useDefaultValueForUnknownEnum, useNullForUnknownEnum); } @@ -280,6 +311,10 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx // 26-Sep-2021, tatu: [databind#1850] Special case where we get "true" integer // enumeration and should avoid use of {@code Enum.index()} if (_isFromIntValue) { + // [databind#3580] Enum (de)serialization in conjunction with JsonFormat.Shape.NUMBER_INT + if (_isShapeNumberInt) { + return _fromInteger(p, ctxt, p.getIntValue()); + } // ... whether to rely on "getText()" returning String, or get number, convert? // For now assume all format backends can produce String: return _fromString(p, ctxt, p.getText()); @@ -327,7 +362,7 @@ private CompactStringObjectMap _resolveCurrentLookup(DeserializationContext ctxt } protected Object _fromInteger(JsonParser p, DeserializationContext ctxt, - int index) + int intValue) throws IOException { final CoercionAction act = ctxt.findCoercionAction(logicalType(), handledType(), @@ -336,13 +371,13 @@ protected Object _fromInteger(JsonParser p, DeserializationContext ctxt, // First, check legacy setting for slightly different message if (act == CoercionAction.Fail) { if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)) { - return ctxt.handleWeirdNumberValue(_enumClass(), index, + return ctxt.handleWeirdNumberValue(_enumClass(), intValue, "not allowed to deserialize Enum value out of number: disable DeserializationConfig.DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS to allow" ); } // otherwise this will force failure with new setting - _checkCoercionFail(ctxt, act, handledType(), index, - "Integer value ("+index+")"); + _checkCoercionFail(ctxt, act, handledType(), intValue, + "Integer value ("+intValue+")"); } switch (act) { case AsNull: @@ -352,14 +387,26 @@ protected Object _fromInteger(JsonParser p, DeserializationContext ctxt, case TryConvert: default: } - if (index >= 0 && index < _enumsByIndex.length) { - return _enumsByIndex[index]; + + // [databind#3580] Enum (de)serialization in conjunction with JsonFormat.Shape.NUMBER_INT + if (_isShapeNumberInt) { + Object numberShape = _lookupByShapeNumberInt.find(String.valueOf(intValue)); + if (numberShape != null) { + return numberShape; + } else { + return ctxt.handleWeirdNumberValue(_enumClass(), intValue, + "Number Int value is not one of expected values", + _lookupByShapeNumberInt.toString()); + } + } + if (intValue >= 0 && intValue < _enumsByIndex.length) { + return _enumsByIndex[intValue]; } if (useDefaultValueForUnknownEnum(ctxt)) { return _enumDefaultValue; } if (!useNullForUnknownEnum(ctxt)) { - return ctxt.handleWeirdNumberValue(_enumClass(), index, + return ctxt.handleWeirdNumberValue(_enumClass(), intValue, "index value outside legal index range [0..%s]", _enumsByIndex.length-1); } @@ -470,7 +517,7 @@ protected Class> _enumClass() { /** * Since 2.16, {@link #_lookupByToString} it is passed via - * {@link #EnumDeserializer(EnumResolver, boolean, EnumResolver, EnumResolver)}, so there is no need for lazy + * {@link #EnumDeserializer(EnumResolver, boolean, EnumResolver, EnumResolver, EnumResolver)}, so there is no need for lazy * initialization. But kept for backward-compatilibility reasons. In case {@link #_lookupByToString} is null. * * @deprecated Since 2.16 diff --git a/src/main/java/com/fasterxml/jackson/databind/util/EnumResolver.java b/src/main/java/com/fasterxml/jackson/databind/util/EnumResolver.java index 3e011bdeb4..d645d0b387 100644 --- a/src/main/java/com/fasterxml/jackson/databind/util/EnumResolver.java +++ b/src/main/java/com/fasterxml/jackson/databind/util/EnumResolver.java @@ -2,6 +2,7 @@ import java.util.*; +import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.AnnotationIntrospector; import com.fasterxml.jackson.databind.DeserializationConfig; import com.fasterxml.jackson.databind.EnumNamingStrategy; @@ -302,6 +303,53 @@ public static EnumResolver constructUsingMethod(DeserializationConfig config, ); } + /** + * Method used when ALL of conditions below are met + *
+ * 1. actual String serialization is indicated using @JsonValue on a method in Enum class AND
+ * 2. Enum class is annotated with `@JsonFormat`
+ *
+ * @since 2.21.0
+ */
+ public static EnumResolver constructUsingNumberShape(DeserializationConfig config, AnnotatedClass annotatedClass, AnnotatedMember accessor)
+ {
+ // prepare data
+ final AnnotationIntrospector ai = config.getAnnotationIntrospector();
+ final boolean isIgnoreCase = config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
+ final Class> enumCls0 = annotatedClass.getRawType();
+ final Class