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.

4 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: