diff --git a/src/main/java/com/fasterxml/jackson/databind/JavaType.java b/src/main/java/com/fasterxml/jackson/databind/JavaType.java index fe3cc8fa2a..2362ba5378 100644 --- a/src/main/java/com/fasterxml/jackson/databind/JavaType.java +++ b/src/main/java/com/fasterxml/jackson/databind/JavaType.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.core.type.ResolvedType; import com.fasterxml.jackson.databind.type.TypeBindings; import com.fasterxml.jackson.databind.type.TypeFactory; +import com.fasterxml.jackson.databind.util.ClassUtil; /** * Base class for type token classes used both to contain information @@ -290,8 +291,8 @@ public boolean isConcrete() { @Override public final boolean isEnumType() { // 29-Sep-2019, tatu: `Class.isEnum()` not enough to detect custom subtypes, - // use this instead -// return Enum.class.isAssignableFrom(_class); + // but for some reason this fix will break couple of unit tests: +// return ClassUtil.isEnumType(_class); return _class.isEnum(); } diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/ClassNameIdResolver.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/ClassNameIdResolver.java index a95c7111b3..e68f2eb5c1 100644 --- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/ClassNameIdResolver.java +++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/ClassNameIdResolver.java @@ -89,7 +89,7 @@ protected JavaType _typeFromId(String id, DatabindContext ctxt) throws IOExcepti protected String _idFrom(Object value, Class cls, TypeFactory typeFactory) { // Need to ensure that "enum subtypes" work too - if (Enum.class.isAssignableFrom(cls)) { + if (ClassUtil.isEnumType(cls)) { if (!cls.isEnum()) { // means that it's sub-class of base enum, so: cls = cls.getSuperclass(); } diff --git a/src/main/java/com/fasterxml/jackson/databind/module/SimpleDeserializers.java b/src/main/java/com/fasterxml/jackson/databind/module/SimpleDeserializers.java index 0a9f443b1b..ba0cbb153b 100644 --- a/src/main/java/com/fasterxml/jackson/databind/module/SimpleDeserializers.java +++ b/src/main/java/com/fasterxml/jackson/databind/module/SimpleDeserializers.java @@ -126,6 +126,10 @@ public JsonDeserializer findEnumDeserializer(Class type, } JsonDeserializer deser = _classMappings.get(new ClassKey(type)); if (deser == null) { + // 29-Sep-2019, tatu: Not 100% sure this is workable logic but leaving + // as is (wrt [databind#2457]. Probably works ok since this covers direct + // sub-classes of `Enum`; but even if custom sub-classes aren't, unlikely + // mapping for those ever requested for deserialization if (_hasEnumDeserializer && type.isEnum()) { deser = _classMappings.get(new ClassKey(Enum.class)); } diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java b/src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java index a3d422ffaf..5863a153ef 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java @@ -421,7 +421,7 @@ protected final JsonSerializer findSerializerByPrimaryType(SerializerProvider } return NumberSerializer.instance; } - if (Enum.class.isAssignableFrom(raw)) { + if (ClassUtil.isEnumType(raw) && raw != Enum.class) { return buildEnumSerializer(prov.getConfig(), type, beanDesc); } return null; diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/JsonValueSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/JsonValueSerializer.java index 6112a7d4cf..77fb8a17f1 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/std/JsonValueSerializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/JsonValueSerializer.java @@ -249,7 +249,7 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t */ final JavaType type = _accessor.getType(); Class declaring = _accessor.getDeclaringClass(); - if ((declaring != null) && declaring.isEnum()) { + if ((declaring != null) && ClassUtil.isEnumType(declaring)) { if (_acceptJsonFormatVisitorForEnum(visitor, typeHint, declaring)) { return; } diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializers.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializers.java index 20dae5df3a..c3c93d5bc4 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializers.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializers.java @@ -100,7 +100,7 @@ public static JsonSerializer getFallbackKeySerializer(SerializationConfi } // 29-Sep-2019, tatu: [databind#2457] can not use 'rawKeyType.isEnum()`, won't work // for subtypes. - if (Enum.class.isAssignableFrom(rawKeyType)) { + if (ClassUtil.isEnumType(rawKeyType)) { return EnumKeySerializer.construct(rawKeyType, EnumValues.constructFromName(config, (Class>) rawKeyType)); } diff --git a/src/main/java/com/fasterxml/jackson/databind/util/ClassUtil.java b/src/main/java/com/fasterxml/jackson/databind/util/ClassUtil.java index 9f0497a44b..c34c30f300 100644 --- a/src/main/java/com/fasterxml/jackson/databind/util/ClassUtil.java +++ b/src/main/java/com/fasterxml/jackson/databind/util/ClassUtil.java @@ -169,7 +169,7 @@ public static String canBeABeanType(Class type) if (type.isArray()) { return "array"; } - if (type.isEnum()) { + if (Enum.class.isAssignableFrom(type)) { return "enum"; } if (type.isPrimitive()) { @@ -954,6 +954,16 @@ public static void checkAndFixAccess(Member member, boolean force) /********************************************************** */ + /** + * Helper method that encapsulates reliable check on whether + * given raw type "is an Enum", that is, is or extends {@link java.lang.Enum}. + * + * @since 2.10.1 + */ + public static boolean isEnumType(Class rawType) { + return Enum.class.isAssignableFrom(rawType); + } + /** * Helper method that can be used to dynamically figure out * enumeration type of given {@link EnumSet}, without having diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestEnumSerialization.java b/src/test/java/com/fasterxml/jackson/databind/ser/TestEnumSerialization.java index eea4609c1a..86e6b08b7f 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ser/TestEnumSerialization.java +++ b/src/test/java/com/fasterxml/jackson/databind/ser/TestEnumSerialization.java @@ -147,10 +147,8 @@ public void testSimple() throws Exception public void testEnumSet() throws Exception { - StringWriter sw = new StringWriter(); - EnumSet value = EnumSet.of(TestEnum.B); - MAPPER.writeValue(sw, value); - assertEquals("[\"B\"]", sw.toString()); + final EnumSet value = EnumSet.of(TestEnum.B); + assertEquals("[\"B\"]", MAPPER.writeValueAsString(value)); } /** @@ -160,9 +158,7 @@ public void testEnumSet() throws Exception */ public void testEnumUsingToString() throws Exception { - StringWriter sw = new StringWriter(); - MAPPER.writeValue(sw, AnnotatedTestEnum.C2); - assertEquals("\"c2\"", sw.toString()); + assertEquals("\"c2\"", MAPPER.writeValueAsString(AnnotatedTestEnum.C2)); } public void testSubclassedEnums() throws Exception