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.

receiveOrNull susceptible to type erasure

See original GitHub issue

https://github.com/ktorio/ktor/blob/84f213a320f38e11ffaafe7b5e7f321a6b7adaab/ktor-server/ktor-server-core/src/io/ktor/request/ApplicationReceiveFunctions.kt#L70

The highlighted code is susceptible to type erasure. If I want to receiveOrNull<Map<String, List<String>>>(), for instance, at runtime I will only have access to Map<*, *>::class. This is a problem for a lot of serializations frameworks, like jackson, who use TypeTokens or the like.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
nikolajakshiccommented, Mar 11, 2019

@tylerbobella Same thing happens with Gson as well. And for workaround, well you can still take the whole string and convert it with jackson/gson manually.

With Gson, instead of:

val items = call.receive<List<MyType>>()

use

val items = Gson().fromJson<List<MyType>>(call.receiveText(), object : TypeToken<List<MyType>>() {}.type)

You can write extension function and make it look prettier.

0reactions
cy6erGn0mcommented, Dec 18, 2019

Since 1.3.0 KType is available (from typeOf() and kotlinx.serialization support should work as well. You may check it in version 1.3.0-rc

Read more comments on GitHub >

github_iconTop Results From Across the Web

unable to use ktor-client with jackson and collections #346
The problem I am encountering is with the suspend inline fun HttpClient.get() function. When the reified T is a List , the type...
Read more >
Understand Kotlin Reified and Type Erasure - Level Up Coding
In Java, and other programming languages, there's a concept known as type erasure. This means that when code is compiled, the type is...
Read more >
Generics and Overcoming Type Erasure on the JVM - Stackify
This is called type erasure, because parameterized types are “erased”. Generics are enforced at compile-time by the compiler itself. For example ...
Read more >
Using Type Erasure to Build a Dependency Injecting Routing ...
Type erasures often only have the initializer exposed -- all usage is controlled internally to guarantee the force-unwraps will never fail. In ...
Read more >
Type Erasure - ModernesCpp.com
Type erasure based on templates is a pretty sophisticated technique. It allows you to bridge dynamic polymorphism (object orientation) with ...
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