Right now I'm getting `ClassCastException` and deb...
# announcements
a
Right now I'm getting
ClassCastException
and debugging shows that
line no. 12
does nothing. What might be the reason, that
inline
version sometimes (for some calls) skips the cast and returns the same object (i.e.
String
) instead of
reified T
? Classic approach with passing class ref works fine.
Callsite looks like this:
Copy code
fun getModel(id: Int: CompletableFuture<Model> {
        val request: HttpRequest = //...
        return sendAsync(request)
    }
q
What's your inline declaration?
a
@Quy D X Nguyen can you specify what's that? There is an inline function in code snippet, also I added call-site usage.
q
Maybe a bug in the readValue method?
a
I guess that problem was that I have a generic over generic type parameter and reified cannot handle this correctly. Solution I came up with:
Copy code
protected inline fun <reified T> sendAsync(
        request: HttpRequest,
        typeReference: TypeReference<T> = jacksonTypeRef()
    ): CompletableFuture<T> {
        return client.sendAsync(request) { _ ->
            BodySubscribers.mapping(BodySubscribers.ofString(StandardCharsets.UTF_8)) {
                mapper.readValue(it, typeReference)
            }
        }.thenApply { it.body() }
    }
looks the same, but instead of simple
reified T
, I'm handing over
TypeReference
as well.
It pretty much looks like this: https://youtrack.jetbrains.com/issue/KT-22798 , so I think it's a bug?