Viktor Orlyk
04/24/2022, 3:56 PMphldavies
04/24/2022, 8:41 PMApplicationCall.respond
takes a reified type parameter which is captured and used for serialization in the ContentNegotiation plugin. As such, the compile-time (not run-time) type of the parameter value is used to fully resolve the serializer to use. If you're passing the body to the .respond
method with only the type BaseResponse<List<User>>
known, only that type information is captured, which isn't enough to pick the right serializer. You can work around this either by casting the value to PaginatedResponse<T>
in the call (i.e. call.respond(response as PaginatedResponse<T>)
or use an extension to take care of resolving all possible types of BaseResponse<T>
:
suspend inline fun <reified T> ApplicationCall.respond(message: BaseResponse<T>) = when(message) {
is PaginatedResponse -> _respond(message)
else -> _respond(message)
}
or possibly better yet would be to change BaseResponse<T>
to be a sealed interface
which might well solve the resolution problem for you.