Description
ktor Server, kotlinx serialization
This issue is a try to draw attention to fixes made with respect to 1879 and partially related to 1858
With the current approach, kotlinx serialization feature is not able to perform serialization of generic classes during the call.respond
.
Suppose you want to formalize responses in a way like this:
@Serializable
data class APIResponse<T>(
val result: T,
val success: Boolean = true,
val errorCode: Int?,
@Serializable(with = DateTimeSerializer::class)
val timestamp: DateTime
)
In that situation serializer of APIResponse
would have an argument, in order to implicitly declare nested serializer:
val serializer = APIResponse.serializer(ContentReview.serializer())
And by calling call.respond
you would receive Can't locate argument-less serializer for class APIResponse
, since with the current fixes module.getContextual(value::class)
returns null.
I guess this problem also can be generalized to the case of call.receive<Boxed<Int>>()
Please consider specifying serializer to use in call.respond
and call.receive
in some way other than module.getContextual(...)
. Unless you want to deal with all of the feature requests related to kotlinx serialization of course.
My suggestion is to add an option to call.respond
/call.receive
in order to specify the serializer for complex cases and use default serializer installed through install(ContentNegotiation)
as a default one.
Activity
cy6erGn0m commentedon Aug 3, 2020
Yes, this is the known limitation that will be eliminated in the future.
lamba92 commentedon Aug 7, 2020
Duplicate of #1783
rwsbillyang commentedon Aug 28, 2020
I am also plagued by this problem. I used the similar design in http response. I have to add 'contextual(MyDataModel.serializer()) ' into SerializersModule for every data model class after upgrade to koltin 1.4.0. It's cumbersome if there are lots of data model classes in module.
e5l commentedon Apr 5, 2021
Duplicate
xht418 commentedon Dec 3, 2021
I got the similar problem today: https://stackoverflow.com/q/70207677/3466808, any one can help?