IndexOutOfBoundsException on Generics Subclass Serialization
See original GitHub issueDescribe 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:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m having the same Problem
Duplicate of #1264