-
Notifications
You must be signed in to change notification settings - Fork 38k
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
Support Kotlin parameter default values in HTTP handler methods #21139
Comments
Alex Rader commented In Kotlin |
Sébastien Deleuze commented As discussed in the PR, current Kotlin API force us to integrate that as a rather involved and not very elegant way, I will raise and discuss that point with Kotlin team. As a consequence, I think it is more reasonable to target 5.2 for that one. |
Is there any updates on this issue? |
I'm also interested in this feature. |
The pull-request was closed in August 2020. |
Resolving the value in Spring (like a defaultValue) is not feasible, because Kotlin's default is not a simple value but a piece of essentially arbitrary object-internal kotlin code, which Spring has no access to. An alternative approach would be to use @jvmoverloads to have kotlin generate explicit permutations of the method signature. Spring would need to select the right one depending on which params are present, in stead of rejecting them all as ambiguous endpoint mappings like it does now. One would still need to define a sane course of action if that annotation is missing. Bad request? In any case: the situation today is that a kotlin endpoint with a default value will run without incident right up until a request without the parameter arrives, in which case it will just throw an NPE from way inside Spring's parameter mapping. That has got to be the wrong approach whichever way you look at it. |
@jlous I would like to understand this issue a bit more. What exactly is required on the Kotlin side to make this work as expected with Spring (no duplicated default values)? Perhaps the ability to access default value via reflection? |
I have opened an issue in Kotlin issue tracker: https://youtrack.jetbrains.com/issue/KT-56893/Provide-a-way-to-read-default-argument-values |
Let's try to see if we can support that in 6.1 (no promise but I will try). |
As discussed in the Kotlin issue, we are probably going to handle this with a refined variant of the original PR. |
I have pushed support for Kotlin parameter default values in handler methods. It allows to write: Both Spring MVC and WebFlux are supported, including on suspending functions. The changes are quite involved, so I would appreciate feedback based on Spring Framework |
This commit fixes a regression introduced by spring-projectsgh-21139 via the usage of Kotlin reflection to invoke HTTP handler methods. It ensures that kotlin.Unit is treated as void by returning null. It also polishes CoroutinesUtils to have a consistent handling compared to the regular case, and adds related tests to prevent future regressions. Closes spring-projectsgh-31648
This change caused a regression in our project related to calling suspend functions with
|
@Quantum64 Please create a dedicated issue with a reproducer. |
@sdeleuze This change caused another regression using extension receivers. Not necessarily a regression, more a bug as the change just completely ignores Kind.EXTENSION_RECEIVER. I think java's lack of exhaustive checks for non-expression |
@efemoney Could you please create a new issue with a reproducer please (attached archive or link to a repository)? |
Sure, let me do that. |
@sdeleuze I see you just fixed this yesterday. Looking forwrd to the release. Sorry I didnt get to creating an issue. |
Take Weiland opened SPR-16598 and commented
Support specifying defaults for e.g.
@RequestMapping
via Kotlin parameter default values like so:Such a method should behave equivalent to
@RequestMapping
withdefaultValue
, except that the value does not need to be processed by converters, etc.This can be implemented in
org.springframework.web.method.support.InvocableHandlerMethod
using Kotlin Reflection (KCallable#callBy
), which allows omitting parameters with default values.Affects: 5.0.4
Referenced from: pull request #1741
2 votes, 5 watchers
The text was updated successfully, but these errors were encountered: