Hello! I’m experiencing difficulties using `inline...
# getting-started
d
Hello! I’m experiencing difficulties using
inline reified
functions. I’m making a generic web client using Spring WebFlux. For example a generic GET fun looks like this:
Copy code
suspend inline fun <reified T> httpGet(uri: String, params: Map<String, Any>): T? {
        return webClient.get()
            .uri(uri, params)
            .exchangeToMono { response ->
                when (response.statusCode()) {
                    HttpStatus.OK -> response.bodyToMono(T::class.java)
                    HttpStatus.NOT_FOUND -> Mono.empty()
                    HttpStatus.BAD_REQUEST ->
                        response.bodyToMono(ErrorResponse::class.java)
                            .flatMap { errorResponse: ErrorResponse ->
                                Mono.error(mapToException(errorResponse))
                            }

                    else -> response.createException().flatMap { Mono.error(it) }
                }
            }
            .timeout(requestTimeout)
            .retryWhen(
                retryPolicy
                    .onRetryExhaustedThrow { _, retrySignal ->
                        RuntimeException("Failed to fetch $uri", retrySignal.failure())
                    },
            )
            .awaitFirstOrNull()
    }
This works great until this function is called within a coroutine scope. In that case
T
seems to not be reified anymore. Am I doing something wrong or is it a bug?
Important note: in the coroutine context it doesn’t work if
T
is a collection. Let’s say I call it like this:
Copy code
client.httpGet<List<Promotion>>(promotionsPath, mapOf("contactId" to contactId.value))
Promotion
object in this case won’t be reified
s
Are you sure it's ever reified?
T::class.java
will produce a
Class
instance that doesn't preserve any generic information. Does it work better if you use the Kotlin-specific
bodyToMono<T>()
function? https://docs.spring.io/spring-framework/docs/current/kdoc-api/spring-webflux/org.springframework.web.reactive.function.client/body-to-mono.html
d
Thank you for your reply! I have a unit test for this specific function and it works outside of a coroutine. I’ll give
bodyToMono<T>()
a try