[SPR-14165] Support Kotlin nullable information in Spring MVC parameters Created: 13/Apr/16  Updated: 15/Jan/19  Resolved: 24/Nov/16

Status: Closed
Project: Spring Framework
Component/s: Web
Affects Version/s: 4.2.5
Fix Version/s: 5.0 M4

Type: Improvement Priority: Minor
Reporter: Raman Gupta Assignee: Sébastien Deleuze
Resolution: Complete Votes: 0
Labels: kotlin, mvc, pull-request-submitted
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
is related to SPR-14951 Support Kotlin nullable information f... Closed
Days since last comment: 9 weeks, 5 days ago
Last commented by a User: true
Last updater: Spring Issuemaster


Spring MVC now supports defining fields with the JDK 8 Optional type to indicate that the parameter is optional (cool!).

Kotlin has fields which can be marked as a nullable type e.g. String? vs. String. This information should be used by Spring MVC in the same way as Optional.

See: KType documentation

Comment by Sébastien Deleuze [ 14/Apr/16 ]

Very good idea! I would maybe suggest to put such Kotlin specific functionalities in https://github.com/sdeleuze/spring-kotlin for now. This would allow us to add it with other various Kotlin related extensions, and evaluate later if we integrate it as an official https://github.com/spring-projects project or not. If that fine to you, could you please create an issue here?

Comment by Raman Gupta [ 18/Apr/16 ]

Ok! Issue created here: https://github.com/sdeleuze/spring-kotlin/issues/1

Comment by Sébastien Deleuze [ 18/Apr/16 ]

Raman Gupta Thanks for creating the other issue, but after discussing with the team we are ok to work on this issue directly as a pull request for https://github.com/spring-projects/spring-framework. Depending on the amount of changes needed, we will evaluate if we can integrate that directly in Spring Framework or not. Are you interested in contributing a PR that I could review using existing Optional support as a template?

Comment by Raman Gupta [ 18/Apr/16 ]

@Sebastien Deleuze, great! I can take a look at writing up a pull request over the next week or two, if the effort required is not too great. One question: is it ok to have/introduce a dependency on Kotlin? I believe I would need to use Kotlin reflection in order to implement this, so the equivalent of the ObjectToOptionalConverter for Kotlin nullables (ObjectToKtNullableConverter ?) would probably be best written in Kotlin, or at least in Java with some type of Kotlin helper for the reflection bits.

Comment by Sébastien Deleuze [ 18/Apr/16 ]

Awesome, thanks. It is fine to introduce an optional dependency in build.gradle, and to enable such feature only when Kotlin is on the classpath, as I did here.

Comment by Raman Gupta [ 14/May/16 ]

@Sébastien Deleuze Please check out this pull request for my draft implementation: https://github.com/spring-projects/spring-framework/pull/1060

Comment by Sébastien Deleuze [ 08/Aug/16 ]

Raman Gupta Thanks for your PR, I have reviewed it and added my comments on GitHub. Please let me know if you can take my feedbacks in account, in order to move forward and see with the team if we can integrate that support in Spring Framework 5.0.

Comment by Raman Gupta [ 08/Aug/16 ]

Thanks for the review. I will take a look over the next few days and update the PR.

Comment by Raman Gupta [ 22/Oct/16 ]

Sébastien Deleuze I've updated the pull request as requested.

Comment by Sébastien Deleuze [ 21/Nov/16 ]

I have polished, refactored and added more tests to your commit via this new PR, and have requested a review from Juergen since these are non-trivial additions.

Comment by Sébastien Deleuze [ 24/Nov/16 ]

I merged a refactored version that reuses MethodParameter#isOptional(), with more tests and polish (see this additional commit)

Comment by Sébastien Deleuze [ 24/Nov/16 ]

Raman Gupta Thanks for your contribution, I will work on @Autowired and @Inject support as part of SPR-14951 (should be part of 5.0.0.M4).

Comment by Raman Gupta [ 24/Nov/16 ]

Sébastien Deleuze Awesome, thanks!

Comment by Spring Issuemaster [ 14/Jan/19 ]

The Spring Framework has migrated to GitHub Issues. This issue corresponds to spring-projects/spring-framework#18737.

Generated at Sat Mar 23 21:12:22 UTC 2019 using JIRA 7.9.2#79002-sha1:3bb15b68ecd99a30eb364c4c1a393359bcad6278.