Skip to content

[Design] Why ContextInjector is an internal API #4720

Description

@GeorgePap-719

What do we have today?

ContextInjector is an internal API.

Why does this feel problematic?

I am required to implement it for creating proper glu-code with the jooq library, which uses its own implementations of the Publisher interface.

@OptIn(InternalCoroutinesApi::class)
class JooqContextInjector : ContextInjector {
    @Suppress("UNCHECKED_CAST")
    override fun <T> injectCoroutineContext(publisher: Publisher<T>, coroutineContext: CoroutineContext): Publisher<T> {
        val reactorContext = coroutineContext[ReactorContext]?.context ?: return publisher
        return when (publisher) {
            is Mono -> publisher.contextWrite(reactorContext)
            is Flux -> publisher.contextWrite(reactorContext)
            // Bridge with jooq:
            // `Flux.from(publisher)` is jooq's documented way to turn its bridge `Publisher` into something Reactor-native.
            // Though, wrapping alone only gets us a real Flux; the context still needs a path into jooq's internals.
            // That path is the `SubscriberProvider` SPI, see `ReactorSubscriberProvider`.
            else -> Flux.from(publisher as Publisher<Any>).contextWrite(reactorContext) as Publisher<T>
        }
    }
}

Is there another way to implement the bridge code without using the ContextInjector API?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions