You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I searched in the issues and found nothing similar.
I searched in the issues of databind and other modules used and found nothing similar.
I have confirmed that the problem only occurs when using Kotlin.
Describe the bug
Multi layer hierarchy could not be deserialized and fails with an error like the following
Cannot construct instance of `com.test.B2` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
To Reproduce
packagecom.testimportcom.fasterxml.jackson.annotation.JsonSubTypesimportcom.fasterxml.jackson.annotation.JsonTypeInfoimportcom.fasterxml.jackson.module.kotlin.jacksonObjectMapperimportorg.junit.jupiter.api.Assertions.assertEqualsimportorg.junit.jupiter.api.TestclassJacksonKotlinModuleIssue {
privateval mapper = jacksonObjectMapper()
@Test
fun`Hierarchy deserialization test`() {
val c1 =C1()
val c2 =C2()
val c3 =C3()
val c4 =C4()
val c1Json = mapper.writeValueAsString(c1)
val c2Json = mapper.writeValueAsString(c2)
val c3Json = mapper.writeValueAsString(c3)
val c4Json = mapper.writeValueAsString(c4)
// worksval c1deserialised = mapper.readValue(c1Json, C1::class.java)
// worksval c2deserialised = mapper.readValue(c2Json, B1::class.java)
// worksval c3deserialised = mapper.readValue(c3Json, B2::class.java)
// failsval c4deserialised = mapper.readValue(c4Json, A::class.java)
assertEquals(c1, c1deserialised)
assertEquals(c2, c2deserialised)
assertEquals(c3, c3deserialised)
assertEquals(c4, c4deserialised)
}
}
@JsonTypeInfo(
use =JsonTypeInfo.Id.NAME,
include =JsonTypeInfo.As.EXISTING_PROPERTY,
property ="type",
visible =true,
)
@JsonSubTypes(
JsonSubTypes.Type(value =B1::class, name ="B1"),
JsonSubTypes.Type(value =B2::class, name ="B2"),
)
sealedclassA {
abstractval type:String
}
@JsonTypeInfo(
use =JsonTypeInfo.Id.NAME,
include =JsonTypeInfo.As.EXISTING_PROPERTY,
property ="subtype",
visible =true,
)
@JsonSubTypes(
JsonSubTypes.Type(value =C1::class, name ="C1"),
JsonSubTypes.Type(value =C2::class, name ="C2"),
)
sealedclassB1(
overridevaltype:String = "B1",
) : A() {
abstractval subtype:String
}
@JsonTypeInfo(
use =JsonTypeInfo.Id.NAME,
include =JsonTypeInfo.As.EXISTING_PROPERTY,
property ="subtype",
visible =true,
)
@JsonSubTypes(
JsonSubTypes.Type(value =C3::class, name ="C3"),
JsonSubTypes.Type(value =C4::class, name ="C4"),
)
sealedclassB2(
overridevaltype:String = "B2",
) : A() {
abstractval subtype:String
}
data classC1(
overridevalsubtype:String = "C1",
) : B1()
data classC2(
overridevalsubtype:String = "C2",
) : B1()
data classC3(
overridevalsubtype:String = "C3",
) : B2()
data classC4(
overridevalsubtype:String = "C4",
) : B2()
Expected behavior
deserialisation works fine for multi-layer hierarchy
Correct: multiple levels with differing @JsonTypeInfo is not supported by jackson-databind.
Since feature itself is not provided by Kotlin module so issue would need to be in jackson-databind (there likely already is an open issue).
There are no current plans to support such feature, fwtw.
Hi @cowtowncoder! Thanks a lot for the reply!
Could you please share the issue? I tried to find anything related to the problem but found very little information.
Search before asking
Describe the bug
Multi layer hierarchy could not be deserialized and fails with an error like the following
To Reproduce
Expected behavior
deserialisation works fine for multi-layer hierarchy
Versions
Kotlin: 1.9
Jackson-module-kotlin: 2.17.2
Jackson-databind: 2.17.2
Additional context
No response
The text was updated successfully, but these errors were encountered: