jackson-databind: Not able to deserialize Enum with default typing after upgrading 2.15.4 -> 2.17.1
Search before asking
- I searched in the issues and found nothing similar.
Describe the bug
After bumping up the version to 2.17.1 I’m not longer able to deserialize values serialized with the version 2.15.4. Moreover, values serialized with the new version (2.17.1) give error when deserializing.
Version Information
2.17.1
Reproduction
package com.kze.test;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder;
import java.io.IOException;
import java.util.EnumSet;
import static com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY;
import static com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping.NON_FINAL;
import static com.kze.test.TestClazz.TestEnum.TEST_ENUM_VALUE;
public class TestClazz {
public enum TestEnum {
TEST_ENUM_VALUE
}
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
final PolymorphicTypeValidator validator = BasicPolymorphicTypeValidator.builder()
.allowIfSubType("com.kze.test")
.allowIfSubType("java")
.build();
ObjectMapper.DefaultTypeResolverBuilder resolverBuilder
= new ObjectMapper.DefaultTypeResolverBuilder(NON_FINAL, validator) {
@Override
public boolean useForType(JavaType t) {
return true;
}
};
StdTypeResolverBuilder stdTypeResolverBuilder = resolverBuilder
.init(JsonTypeInfo.Id.CLASS, null)
.inclusion(PROPERTY);
mapper.setDefaultTyping(stdTypeResolverBuilder);
mapper.configure(JsonParser.Feature.INCLUDE_SOURCE_IN_LOCATION, true);
EnumSet<TestEnum> input = EnumSet.of(TEST_ENUM_VALUE);
String inputJson = mapper.writeValueAsString(input);
System.out.println(inputJson);
Object inputDeserialized = mapper.readValue(inputJson, Object.class);
System.out.println(inputDeserialized);
}
}
Version 2.15.4 output
["java.util.EnumSet<com.kze.test.TestClazz$TestEnum>",["TEST_ENUM_VALUE"]]
[TEST_ENUM_VALUE]
Version 2.17.1 error:
["java.util.EnumSet<com.kze.test.TestClazz$TestEnum>",["TEST_ENUM_VALUE"]]
Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (VALUE_STRING), expected START_ARRAY: need Array value to contain `As.WRAPPER_ARRAY` type information for class com.kze.test.TestClazz$TestEnum
at [Source: (String)"["java.util.EnumSet<com.kze.test.TestClazz$TestEnum>",["TEST_ENUM_VALUE"]]"; line: 1, column: 56] (through reference chain: java.util.RegularEnumSet[0])
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.jackson.databind.DeserializationContext.wrongTokenException(DeserializationContext.java:1913)
at com.fasterxml.jackson.databind.DeserializationContext.reportWrongTokenException(DeserializationContext.java:1699)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._locateTypeId(AsArrayTypeDeserializer.java:141)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:96)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromScalar(AsArrayTypeDeserializer.java:66)
at com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer.deserializeWithType(StdScalarDeserializer.java:66)
at com.fasterxml.jackson.databind.deser.std.EnumSetDeserializer._deserialize(EnumSetDeserializer.java:281)
at com.fasterxml.jackson.databind.deser.std.EnumSetDeserializer.deserialize(EnumSetDeserializer.java:246)
at com.fasterxml.jackson.databind.deser.std.EnumSetDeserializer.deserialize(EnumSetDeserializer.java:24)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:120)
at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromArray(AsArrayTypeDeserializer.java:53)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromAny(AsPropertyTypeDeserializer.java:238)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializerNR.deserializeWithType(UntypedObjectDeserializerNR.java:112)
at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:74)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4905)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3848)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3816)
at com.kze.test.TestClazz.main(TestClazz.java:52)
Expected behavior
Should be able to deserialized values
Additional context
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 18 (10 by maintainers)
Commits related to this issue
- [SPARK-51927][SPARK-52213][BUILD] Upgrade jackson to 2.19.0 and upgrade kubernetes-client to version 7.3.0 ### What changes were proposed in this pull request? The primary objective of this pr is to ... — committed to apache/spark by LuciferYang 2 years ago
- [SPARK-51927][SPARK-52213][BUILD] Upgrade jackson to 2.19.0 and upgrade kubernetes-client to version 7.3.0 ### What changes were proposed in this pull request? The primary objective of this pr is to ... — committed to apache/spark by LuciferYang 2 years ago
- [SPARK-51927][SPARK-52213][BUILD] Upgrade jackson to 2.19.0 and upgrade kubernetes-client to version 7.3.0 ### What changes were proposed in this pull request? The primary objective of this pr is to ... — committed to yhuang-db/spark by LuciferYang 2 years ago
- [SPARK-51927][SPARK-52213][BUILD] Upgrade jackson to 2.19.0 and upgrade kubernetes-client to version 7.3.0 ### What changes were proposed in this pull request? The primary objective of this pr is to ... — committed to yhuang-db/spark by LuciferYang 2 years ago
@cowtowncoder right, I thought 2.17 version is closed for some reason 🙉. Let’s talk versions when you get back! 👍🏼
Thank you for sharing workaround @lenrok258! 🙏🏼🙏🏼 Hopefully I get to find solution tonight (taking a shot at it) 🥲
Just to let you know, applying @JsonTypeInfo(use = NONE) on the enum declaration instead of on EnumSet property seems to be working fine.
Reading
doesn’t throw Exception in newer versions of Jackson so it could be treated as a good workaround for now.
Unformtunately as the json in db has the
java.util.EnumSet<com.kze.test.TestClazz$TestEnum>as the type I’m not able to deserialize it into Set.I think my only option is to write a migration code which takes all the values from the db and saves them again with EnumSet->Set migrated.
I’ve tested it with the 2.18.2 and it’s still not working.
What I found out is the problematic bit is the EnumSet. If I replace it with regular Set it works perfectly fine with all the versions! In my example attached to this ticket if you change the line
to
mapper is able to deserialize json, no exception is thrown and the output is:
So I guess it is rather rare case as it needs to have EnumSet with enum values serialized and deserialized with default typing enabled.
Nevertheless there is a decadency between serializer (decides not to include type as a index 0) and deserializer which, in such case, requires type at the beginning of the array.
[edit] Not having type for enum in EnumSet seems sensible as there’s already an enum type as a generic type in EnumSet.
My assumption is that deserializer logic is too strict thus throwing Exception even if the type could be inferred from EnumSet