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.

Support multiple class descriminators for polymorphism.

See original GitHub issue

What is your use-case and why do you need this feature? As of writing this issue, only a single classDiscriminator can be specified on a Json object. I’m working with a REST api where for different endpoints or error codes, the object returned will use a different “class discriminator”.

For example, if the response code is != 200, then an error in form of JSON is returned.

The discriminator here is errcode.

sealed class MatrixError : Throwable() {
    abstract val errcode: String
}

@Serializable
@SerialName("M_NOT_FOUND")
data class NotFound(override val error: String) : MatrixError() {
    override val errcode get() = "M_NOT_FOUND"
}

@Serializable
@SerialName("M_LIMIT_EXCEEDED")
data class LimitExceeded(
    override val error: String,

    @SerialName("retry_after_ms")
    val retryAfterMillis: Long
) : MatrixError() {
    override val errcode get() = "M_LIMIT_EXCEEDED"
}

If the response code is 200, then the model uses type as the discriminator.

sealed class Event

@SerialName("m.room")
data class RoomEvent : Event

@SerialName("m.join")
data class JoinEvent(val userId: String) : Event

Describe the solution you’d like I would like to be able to annotate the parent class with a discriminator.

Or supply a class discriminator in the module like so.

SerializersModule {
    polymorphic<MatrixError>(discriminator="errcode") {
        addSubclass(MatrixError.Unknown.serializer())
        addSubclass(MatrixError.NotFound.serializer())
        addSubclass(MatrixError.Forbidden.serializer())
        addSubclass(MatrixError.UnsupportedRoomVersion.serializer())
        addSubclass(MatrixError.LimitExceeded.serializer())
   }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:53
  • Comments:19 (10 by maintainers)

github_iconTop GitHub Comments

4reactions
kroegeramacommented, Jun 18, 2021

Will this be included in the next release? I’m really looking forward to it.

4reactions
sandwwraithcommented, May 24, 2021

@KamilJanda It is planned and in progress now

Read more comments on GitHub >

github_iconTop Results From Across the Web

Polymorphism - GitHub Pages
When you have a class hierarchy and will be serializing instances of varying classes to the same collection you need a way to...
Read more >
Polymorphism — PynamoDB 5.3.4 documentation
PynamoDB supports polymorphism through the use of discriminators. A discriminator is a value that is written to DynamoDB that identifies the python class...
Read more >
Kotlin serialization: nested polymorphic module - Stack Overflow
Recently I've been trying to implement Kotlinx Serialization for polymorphic class hierarchy. I used this guide, however my example was a ...
Read more >
Inheritance and Polymorphism - Swagger
To help API consumers detect the object type, you can add the discriminator/propertyName keyword to model definitions. This keyword points to the property ......
Read more >
Mapping Class Inheritance Hierarchies — SQLAlchemy 1.4 ...
While a polymorphic discriminator expression is not strictly necessary, it is required if polymorphic loading is desired. Establishing a simple ...
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