Affects: \
- spring-web 6.0.11
I'm using Spring WebFlux. I would like to be able to write to Context (ReactorContext) from CoWebFilter.
ReactorContext is implemented immutably, so if we can't write it like WebFilter, we won't be able to easily write it.
WebFilter example:
class ExampleWebFilter : WebFilter {
override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
return chain
.filter(exchange)
.contextWrite { ctx -> ctx.put("hoge", "fuga") }
}
}
I want to write in a similar way using CoWebFilter.
Comment From: sdeleuze
Can you check if https://github.com/spring-projects/spring-framework/issues/27522 allow to do what you need?
Comment From: sh-ogawa
Hi. Thank you for your reply. I tried the following code and working correctly.
@Component
class TenantEncryptionKeyFilter : CoWebFilter() {
override suspend fun filter(exchange: ServerWebExchange, chain: CoWebFilterChain) {
val context = Context.of("hoge", "fuga").asCoroutineContext()
withContext(context) {
chain.filter(exchange)
}
}
}
I have one question. CoWebFilter version is old, does it match your recognize?
CoWebFilter
final override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
return mono(Dispatchers.Unconfined) {
filter(exchange, object : CoWebFilterChain {
override suspend fun filter(exchange: ServerWebExchange) {
// exchange.attributes[COROUTINE_CONTEXT_ATTRIBUTE] = currentCoroutineContext().minusKey(Job.Key)
chain.filter(exchange).awaitSingleOrNull()
}
})}.then()
}
Comment From: sdeleuze
Not sure why exchange.attributes[COROUTINE_CONTEXT_ATTRIBUTE] = currentCoroutineContext().minusKey(Job.Key)
is commented, but except that, yes. This is supported as of Spring Framework 6.1.0-M5
and will be part of the upcoming Spring Boot 3.2.0 release. From what I understand, it looks like your use case is covered, so I close this issue.