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

Support Kotlin parameter default values in HTTP handler methods [SPR-16598] #21139

spring-projects-issues opened this issue Mar 15, 2018 · 6 comments


Copy link

@spring-projects-issues spring-projects-issues commented Mar 15, 2018

Take Weiland opened SPR-16598 and commented

Support specifying defaults for e.g. @RequestMapping via Kotlin parameter default values like so:

fun get(@RequestParam limit: Int = 20)

Such a method should behave equivalent to @RequestMapping with defaultValue, except that the value does not need to be processed by converters, etc.

This can be implemented in 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

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Mar 30, 2018

Alex Rader commented

In Kotlin @RequestBody required=false when use default arguments

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jul 7, 2018

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.

Copy link

@AlexTrotsenko AlexTrotsenko commented May 25, 2020

Is there any updates on this issue?

Copy link

@cmdjulian cmdjulian commented Jan 24, 2021

I'm also interested in this feature.

Copy link

@jlous jlous commented Feb 5, 2021

The pull-request was closed in August 2020.

Copy link

@jlous jlous commented Feb 5, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants