question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jackson serialization is not working properly with polymorphic types stored in a collection

See original GitHub issue

Hi,

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:closed
  • Created 4 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
alphahocommented, Dec 30, 2019

Great! Thanks for your help on the PR as well! =D

1reaction
daviddentoncommented, Nov 24, 2019

Releasing in 3.199.0 😄

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found