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.

IndexOutOfBoundsException on Generics Subclass Serialization

See original GitHub issue

Describe the bug I’m trying to create a generic class called Event that takes in a generic serializable payload and can be converted into Json. I’d prefer this so I can eventually pass the code into a function that takes in an object of type Event and serializes it for a call. Something like this:

object EventPublisher {
    private val client = HttpClient(CIO)

    suspend fun <reified T>  publish(event: Event<T>) {
        client.post("http://127.0.0.1:8080/events") {
            setBody(event)
        }
    }
}

so I can publish events of all types from my app.

To Reproduce The following is what gives the error:

@Serializable
sealed class Event<T>(
    val message: String,
    val payload: T
) {
    
    @Serializable
    data class UserCreated(
        val user: User,
    ) : Event<User>(
        "User created",
        user
    )
}

@Serializable
data class User(
    val id: String,
    val name: String
)

fun main() {
    val user = User("id", "Alice")
    println(Json.encodeToString(Event.UserCreated(user)))
}

The two following code snippets seem identical to the one above, but seem to not cause any error:

@Serializable
sealed class Event<T>(
    val message: String,
    val payload: T
) {
    @Serializable
    class UserCreated<T>(
        private val user: T
    ) : Event<T>("StringEvent", user) where T : User
}


@Serializable
data class User(
    val id: String,
    val name: String
)

fun main() {
    val user = User("id", "Alice")
    println(Json.encodeToString(Event.UserCreated(user)))
}
@Serializable
sealed class Event<T>(
    val message: String,
) {
    abstract val payload: T

    @Serializable
    data class UserCreated(
        val user: User,
    ) : Event<User>(
        "User created"
    ) {
        override val payload = user
    }
}

@Serializable
data class User(
    val id: String,
    val name: String
)

fun main() {
    val user = User("id", "Alice")
    println(Json.encodeToString(Event.UserCreated(user)))
}

Expected behavior Compile and work just like the working examples I provided.

Environment

  • Kotlin version: 1.6.10
  • Kotlin platforms: JVM
  • Gradle version: 7.2
  • Plugin: id("org.jetbrains.kotlin.plugin.serialization") version "1.6.20-M1"
  • Dependencies:
implementation(platform("io.ktor:ktor-bom:2.0.0-beta-1"))
implementation("io.ktor:ktor-client-core")
implementation("io.ktor:ktor-client-content-negotiation")
implementation("io.ktor:ktor-client-serialization")
implementation("io.ktor:ktor-client-cio")

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JHarzenettercommented, Jul 7, 2022

I’m having the same Problem

0reactions
sandwwraithcommented, Oct 4, 2022

Duplicate of #1264

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin serialization of a generic interface - Stack Overflow
Use kotlinx.serialization 1.2.0, which allows to declare contextual serializer for generic classes, so your module should become: @ ...
Read more >
RuntimeException (Java Platform SE 7 ) - Oracle Help Center
RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor's throws clause if...
Read more >
Generics serialization | Page 2 - Unity Forum
It just show an error message if I try to add `SerializeReference` on a field of generic type. "This is not supported. You...
Read more >
Java Programming Tutorial on Generics
3.9 Generic Subtypes ... When we try to upcast ArrayList<String> to ArrayList<Object> , it trigger a compilation error "incompatible types". This is because ......
Read more >
Java Generics Examples
Check out our detailed example on Java Generics and Generic Methods! ... of class A to another collection of a subclass or superclass...
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