spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

Support Kotlin Context Parameters

Open SIMULATAN opened this issue 4 months ago • 4 comments

Note: finding a commit message for this change is quite difficult, the current one is likely to cause confusion. I'd appreciate better suggestions

Originally introduced as Beta in 2.2.0, Context Parameters are a new Kotlin feature that I wanted to use in my project. Those parameters have a different KParameter.Kind (CONTEXT) and are therefore not supported by the current CoroutinesUtils implementation. This resulted in the following error message:

java.lang.IllegalArgumentException: No argument provided for a required parameter: context parameter tenant of context(tenant: com.application.TenantContext)

Status

~~kotlin-reflect's Context Parameters support was first released in 2.2.20-Beta1. Therefore, I had to target the latest 2.2.20 RC with this PR.~~ ~~Once 2.2.20 is formally released, I'll await the update on Spring's main branch, rebase onto it, and set the PR as ready to merge.~~ With 86fb62c05998f776e42b7960d9c039f00645ebb2, main is using the latest Kotlin release. I've done the rebase and marked the PR as ready to review. Thanks in advance for your considerations!

SIMULATAN avatar Sep 05 '25 13:09 SIMULATAN

Could you please add related test(s)?

sdeleuze avatar Sep 11 '25 08:09 sdeleuze

Added tests in a fixup commit, analogous to the existing Kotlin Extension Function tests. Had to force-push once as I messed up the GPG signing on my work device.

Let me know in case any further modifications are wanted.

SIMULATAN avatar Sep 17 '25 15:09 SIMULATAN

As mention in #35134, I prefer not rushing a support in 7.0, so I have planned merging this PR for 7.1, along with the KotlinReflectionParameterNameDiscoverer changes, documentation, etc.

@SIMULATAN Could you please share a reproducer for an application using this feature? I would like to fully understand the various use cases of Kotlin context parameters with Spring, so that would help to have for example a controller with suspending functions using this feature, in addition to the tests you provided.

sdeleuze avatar Oct 29 '25 07:10 sdeleuze

I prefer not rushing a support in 7.0, so I have planned merging this PR for 7.1

Totally fair. Perhaps more people will see and try out this PR, hopefully providing additional feedback in the process.

Could you please share a reproducer for an application using this feature? I would like to fully understand the various use cases of Kotlin context parameters with Spring, so that would help to have for example a controller with suspending functions using this feature, in addition to the tests you provided.

I've implemented a simple Todo-like application showcasing our use case: multitenancy. Unfortunately, there's still some nasty glue involved. Thankfully, it is only necessary on the two ends - the controller, using a CoWebFilter, and Hibernate, by using a CurrentTenantIdentifierResolver backed by a ThreadLocal - the most dangerous part of the entire application. Nevertheless, the risk of losing values through thread context switches and other tomfoolery in our business logic is greatly reduced and, quite frankly, it just feels way more reliable and explicit.

Here's the repo: https://github.com/SIMULATAN/spring-context-parameter-demo Although my creativity was limited, it should function as a comprehensible PoC regardless.

SIMULATAN avatar Oct 31 '25 11:10 SIMULATAN