Jackson serialization is not working properly with polymorphic types stored in a collection
See original GitHub issueHi,
I’ve found that the current ConfigurableJackson.autoBody
implementation would not work with collections of polymorphic types. It would lose the @class
field in the serialized json in the below example:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "@class")
@JsonSubTypes(
JsonSubTypes.Type(value = FirstChild::class, name = "first"),
JsonSubTypes.Type(value = SecondChild::class, name = "second")
)
sealed class PolymorphicParent
data class FirstChild(val something: String): PolymorphicParent()
data class SecondChild(val somethingElse: String): PolymorphicParent()
class SampleTest {
@Test
fun `roundtrip polymorphic object to and from body`() {
val body = Body.auto<PolymorphicParent>().toLens()
val firstChild: PolymorphicParent = FirstChild("hello")
val secondChild: PolymorphicParent = SecondChild("world")
assertThat(body(Response(OK).with(body of firstChild)), equalTo(firstChild))
assertThat(body(Response(OK).with(body of secondChild)), equalTo(secondChild))
}
@Test
fun `roundtrip list of polymorphic objects to and from body`() {
val body = Body.auto<List<PolymorphicParent>>().toLens()
val list = listOf(FirstChild("hello"), SecondChild("world"))
assertThat(body(Response(OK).with(body of list)), equalTo(list))
}
}
While the first test(one without being stored in a collection) works, the second test would get:
Caused by: com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class org.http4k.format.PolymorphicParent]: missing type id property '@class'
at [Source: (String)"[{"something":"hello"},{"somethingElse":"world"}]"; line: 1, column: 22] (through reference chain: java.util.ArrayList[0])
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Why does Jackson polymorphic serialization not work in lists?
I'm doing polymorphic serialization and it works perfectly when an object is on its own. But if you put the same object into...
Read more >Polymorphic deserialization not working for this case
We want to be able to serialize Message to JSON and then deserialize it without necessarily referencing Message.class, i.e. we want to go...
Read more >Jackson Tips: custom List serialization | by @cowtowncoder
Overriding serialization of structured/container types like Map and ... No polymorphism ( String is not polymorphic and List typically ...
Read more >Jackson - ObjectMapper Class - Tutorialspoint
Jackson - ObjectMapper Class, ObjectMapper is the main actor class of Jackson ... needed for proper deserialization of polymorphic types (unless types have ......
Read more >JSON serialization - Immutables
Helper classes to integrate Gson streaming with Jackson streaming to squeeze maximum ... annotated with @JsonProperty so deserialization will work properly.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Great! Thanks for your help on the PR as well! =D
Releasing in 3.199.0 😄