Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-15065

Provide a Kotlin DSL for the functional Web API

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: None
    • Fix Version/s: 5.0 M5
    • Component/s: None
    • Labels:
    • Last commented by a User:
      true

      Description

      It would be nice to find a way to avoid specifying explicitly HandlerFunction lambda type for Kotlin user since they won't be able to fix this for Kotlin 1.1 (see this explanation on KT-14923).

      Possible solutions are :

      • Provide Kotlin specific API for the router
      • Provide Kotlin extensions for RouterFunction and RouterFunctions
      • Modify Java API signature to avoid this issue (unlikely but this is a possible solution)

        Issue Links

          Activity

          Hide
          yevhenii-melnyk Yevhenii Melnyk added a comment - - edited

          Sébastien Deleuze, there are some deficiencies in docs for this feature:

          • Nested routes in accept method or chaining two GET's together won't work without explicit import.
            import org.springframework.web.reactive.function.server.RequestPredicates.*. IDE can hardly find solution so from my point of view this should be mentioned somehow.
          • The method reference noted in javadoc shows only reference to the method outside any class. Most likely the method is located inside class to use injected properties so the reference will look like this [email protected]::someMethod
          Show
          yevhenii-melnyk Yevhenii Melnyk added a comment - - edited Sébastien Deleuze, there are some deficiencies in docs for this feature: Nested routes in accept method or chaining two GET's together won't work without explicit import. import org.springframework.web.reactive.function.server.RequestPredicates.* . IDE can hardly find solution so from my point of view this should be mentioned somehow. The method reference noted in javadoc shows only reference to the method outside any class. Most likely the method is located inside class to use injected properties so the reference will look like this [email protected]::someMethod
          Hide
          sdeleuze Sébastien Deleuze added a comment -

          Yevhenii Melnyk Fixed via this commit + I have integrated the new json(), html() and xml() shortcuts.

          Do you see a way to be able to write:

          json {
              (GET("/api/user/") or GET("/api/users/")) { findAll() }
              POST("/api/user/", [email protected]::create)
          }
          

          Instead of what we currently support with this new commit:

          json().apply {
              (GET("/api/user/") or GET("/api/users/")) { findAll() }
              POST("/api/user/", [email protected]::create)
          }
          

          Show
          sdeleuze Sébastien Deleuze added a comment - Yevhenii Melnyk Fixed via this commit + I have integrated the new json() , html() and xml() shortcuts. Do you see a way to be able to write: json { (GET("/api/user/") or GET("/api/users/")) { findAll() } POST("/api/user/", [email protected]::create) } Instead of what we currently support with this new commit: json().apply { (GET("/api/user/") or GET("/api/users/")) { findAll() } POST("/api/user/", [email protected]::create) }
          Hide
          sdeleuze Sébastien Deleuze added a comment -

          Also keep in mind that the nested routing support we are about to start working on with Arjen Poutsma may help to support that more easily, so that's maybe better to wait SPR-14954 to be fixed before digging into that.

          Show
          sdeleuze Sébastien Deleuze added a comment - Also keep in mind that the nested routing support we are about to start working on with Arjen Poutsma may help to support that more easily, so that's maybe better to wait SPR-14954 to be fixed before digging into that.
          Hide
          yevhenii-melnyk Yevhenii Melnyk added a comment -

          I've investigated the issue with json().apply and it seems the only way to do this curretly is like this:

          fun json(f: RequestPredicate.() -> Unit) {
          	RequestPredicates.json().apply(f)
          }
          

          The most right way to build dsl seems to use invoke on RouterFunction but it seems I've stuck with KT-14923.
          The following code doesn't work(ambiguous call) but it would be great if it works some day:

          class RouterDsl {
          ...
          val html: RequestPredicate
          	get() = RequestPredicates.html()
           
          operator fun RequestPredicate.invoke(f: RequestPredicate.() -> Unit): RequestPredicate = this.apply(f)
          

          Show
          yevhenii-melnyk Yevhenii Melnyk added a comment - I've investigated the issue with json().apply and it seems the only way to do this curretly is like this: fun json(f: RequestPredicate.() -> Unit) { RequestPredicates.json().apply(f) } The most right way to build dsl seems to use invoke on RouterFunction but it seems I've stuck with KT-14923 . The following code doesn't work(ambiguous call) but it would be great if it works some day: class RouterDsl { ... val html: RequestPredicate get() = RequestPredicates.html()   operator fun RequestPredicate.invoke(f: RequestPredicate.() -> Unit): RequestPredicate = this .apply(f)
          Hide
          sdeleuze Sébastien Deleuze added a comment - - edited

          Nested routing now works including with pathPrefix() predicate and with any level of nested routes, see this example.

          Show
          sdeleuze Sébastien Deleuze added a comment - - edited Nested routing now works including with pathPrefix() predicate and with any level of nested routes, see this example .

            People

            • Assignee:
              sdeleuze Sébastien Deleuze
              Reporter:
              sdeleuze Sébastien Deleuze
              Last updater:
              Stéphane Nicoll
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                30 weeks, 4 days ago