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.

kotlinx.serialization: issue with multi-level sealed class

See original GitHub issue

Say you need to insertMany elements of a sealed class from a list, you’ll just pass it and I’d expect the field/key type (or ___type as modified by KMongo) to be present in the root of each document.

Since com.mongodb.internal.operation.Operations::insertMany at line 360 treats the serialization of objects as single elements instead of a whole list, the serializer used is the one of the single elements, thus loosing the generation field type.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
zigzagocommented, Nov 26, 2020

There is a bug with kotlinx.serialization and sealed class with more than one level. Workaround: remove the sealed mention except on the root class:

sealed class OuterSealedClass {
     abstract class InnerSealedClass : OuterSealedClass() {
        class A : InnerSealedClass()
        class B : InnerSealedClass()
    }
    class C : OuterSealedClass ()
}
0reactions
zigzagocommented, Nov 27, 2020

see #248

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inconsistent behaviour with sealed classes serialization #1194
I faced a problem with sealed classes serialization. To Reproduce import kotlinx.serialization.Serializable import kotlinx.serialization.
Read more >
can I use kotlinx serializer with multiple sealed class levels as ...
Every time I try to run this I get an error... Class 'MyContent' is not registered for polymorphic serialization in the scope of...
Read more >
Third-party sealed classes serialization with `kotlinx ... - Medium
This is unfortunate because it is impossible to annotate 3rd party subclasses, they are simply out of reach. There are essentially two ways...
Read more >
Serialization for sealed classes gives different results when ...
Serialization for sealed classes gives different results when used through Ktor. Steps to reproduce the behavior: Create class with list:
Read more >
[Solved]-can I use kotlinx serializer with multiple sealed class ...
Coding example for the question can I use kotlinx serializer with multiple sealed class levels as parents and a nested invocation?-kotlin.
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