https://kotlinlang.org logo
Title
d

Dominaezzz

04/27/2021, 5:57 PM
interface Animal

@Serializable
class Dog : Animal

@Serializable
class Cat : Animal

// subclasses are explicitly registered in serializerModule.

client.get<...>("...") {
    contentType(Json)
    body = Dog()
}
How do I get ktor to do polymorphic serialisation correctly?
body
ends up being
{}
instead of
{ "type": "Dog" }
.
b

Big Chungus

04/27/2021, 6:33 PM
I think you need sealed classes for that
Also @Serializable
d

Dominaezzz

04/27/2021, 6:39 PM
I do have
@Serializable
, was just impatient when writing the code lol.
b

Big Chungus

04/27/2021, 6:40 PM
But not sealed classes. Last I checked polymorphism was only supported on sealed hierarchies
d

Dominaezzz

04/27/2021, 6:41 PM
It's supported everywhere now. Unless you're talking about ktor.
b

Big Chungus

04/27/2021, 6:42 PM
No, I was talking about serialisation itself. Good to know then and I'm out of my depth here 😀
d

Dominaezzz

04/27/2021, 6:42 PM
Haha, thanks for trying though. Looks like a bug in ktor.
a

alex009

04/28/2021, 2:01 AM
moko-network support allOf,anyOf,oneOf and it implemented with generation of special serializers, like this - https://github.com/icerockdev/moko-network/blob/master/network/src/commonTest/kotlin/OneOfTest.kt#L122