Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write the ReactorContext from CoWebFilter #31319

Closed
sh-ogawa opened this issue Sep 27, 2023 · 3 comments
Closed

Write the ReactorContext from CoWebFilter #31319

sh-ogawa opened this issue Sep 27, 2023 · 3 comments
Assignees
Labels
status: duplicate A duplicate of another issue theme: kotlin An issue related to Kotlin support

Comments

@sh-ogawa
Copy link

Affects: <Spring Framework version>

  • 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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 27, 2023
@sdeleuze sdeleuze self-assigned this Oct 8, 2023
@sdeleuze sdeleuze added the theme: kotlin An issue related to Kotlin support label Oct 8, 2023
@sdeleuze
Copy link
Contributor

sdeleuze commented Oct 8, 2023

Can you check if #27522 allow to do what you need?

@sdeleuze sdeleuze added the status: waiting-for-feedback We need additional information before we can continue label Oct 8, 2023
@sh-ogawa
Copy link
Author

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()
    }

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Oct 11, 2023
@sdeleuze
Copy link
Contributor

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.

@sdeleuze sdeleuze closed this as not planned Won't fix, can't repro, duplicate, stale Oct 12, 2023
@sdeleuze sdeleuze added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue theme: kotlin An issue related to Kotlin support
Projects
None yet
Development

No branches or pull requests

3 participants