Polymorphic serialization doesn't work for deeper sealed class hierarchies

I have the following code:

@Serializable
sealed class Component : Tagged<UUID, Component> {

    @Serializable @SerialName("name")
    data class Name(val name: String, @Transient override val tag: Tag<Component> = Tag.Name) : Component()

    @Serializable @SerialName("parent")
    data class Parent(val parent: UUID, @Transient  override val tag: Tag<Component> = Tag.Parent) : Component()

    @Serializable @SerialName("entity_type")
    sealed class EntityType : Component() {
        @Transient override val tag = Tag.EntityType

        @Serializable @SerialName("department") object Department : EntityType()
        @Serializable @SerialName("function")  object Function : EntityType()
    }

    @Serializable @SerialName("ordered_children")
    data class OrderedChildren(val children: List<UUID> = listOf(), override val tag: Tag<Component> = Tag.OrderedChildren) : Component()
}

However, when serializing I get the following error:

Class 'Department' is not registered for polymorphic serialization in the scope of 'Component'.
Mark the base class as 'sealed' or register the serializer explicitly.

Why is that? Because ‘Department’ is clearly in the sealed class hierarchy.

This works though:

 val module = SerializersModule {
            polymorphic(Component::class) {
                subclass(Component.EntityType.Department::class)
                subclass(Component.EntityType.Function::class)
            }
        }

but that seems unnecessary to me if I have a strictly sealed hierarchy.

6 Likes

This seems to be a shortcoming/bug(missing feature) in the library (or format), where it doesn’t handle nested sealed types.

@avwie , I’ve just run into the same issue, it still exists in the latest version :neutral_face:
Thanks for the workaround, it fixed the problem :slight_smile:

Just stumbled into this issue as well. I was thinking on writing a custom JsonContentPolymorphicSerializer for that. I will try the workaround. Want to be subscribed to this topic.

So, my case is a bit different. Here is my class structure. Check the inclusion of JsonClassDiscriminator in the base class Component and also in nested base class EntityType. The fields type and sub_type both will be present in the sub classes of EntityType. I get an error on nested base class saying

Argument values for inheritable serial info annotation 'JsonClassDiscriminator' must be the same as the values in parent type 'Component'

is there any workarounds for this issue?

@Serializable
@JsonClassDiscriminator("base_type")
sealed class Component : Tagged<UUID, Component> {

    @Serializable @SerialName("name")
    data class Name(val name: String, @Transient override val tag: Tag<Component> = Tag.Name) : Component()

    @Serializable @SerialName("parent")
    data class Parent(val parent: UUID, @Transient  override val tag: Tag<Component> = Tag.Parent) : Component()

    @Serializable
    **@JsonClassDiscriminator("sub_type")**
    sealed class EntityType : Component() {
        @Serializable @SerialName("department") data class Department : EntityType()
        @Serializable @SerialName("function")  data class Function : EntityType()
    }

    @Serializable @SerialName("ordered_children")
    data class OrderedChildren(val children: List<UUID> = listOf(), override val tag: Tag<Component> = Tag.OrderedChildren) : Component()
}