From 2eb631aaadfc3e9e13370e3256afb3e84bd3f0e1 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Thu, 4 Apr 2019 11:56:20 +0200 Subject: [PATCH] Avoid exposing (Co)RouterFunctionDsl#invoke WebFlux.fn RouterFunctionDsl#invoke and CoRouterFunctionDsl#invoke were wrongly exposed on public API and have never been designed to be used by end users, but rather only invoked from router { } or coRouter { } builders. To fix that, avoiding this method being accessible from the DSL and for the sake of consistency with WebMvc.fn RouterFunctionDsl, Spring Framework 5.2 turns public fun invoke() method to an internal fun build() one. As a consequence RouterFunctionDsl and CoRouterFunctionDsl are not open anymore, they are expected to be extended via Kotlin extensions if needed. Closes gh-22736 --- .../function/server/CoRouterFunctionDsl.kt | 30 +++++++++---------- .../function/server/RouterFunctionDsl.kt | 14 ++++----- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDsl.kt b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDsl.kt index 3a861daf9d..5beed8e41c 100644 --- a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDsl.kt +++ b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDsl.kt @@ -26,13 +26,22 @@ import org.springframework.http.MediaType import org.springframework.web.reactive.function.server.RouterFunctions.nest import java.net.URI +/** + * Coroutines variant of [router]. + * + * @author Sebastien Deleuze + * @since 5.2 + */ +fun coRouter(routes: (CoRouterFunctionDsl.() -> Unit)) = + CoRouterFunctionDsl(routes).build() + /** * Coroutines variant of [RouterFunctionDsl]. * * @author Sebastien Deleuze * @since 5.2 */ -open class CoRouterFunctionDsl(private val init: (CoRouterFunctionDsl.() -> Unit)): () -> RouterFunction { +class CoRouterFunctionDsl(private val init: (CoRouterFunctionDsl.() -> Unit)) { private val builder = RouterFunctions.route() @@ -104,7 +113,7 @@ open class CoRouterFunctionDsl(private val init: (CoRouterFunctionDsl.() -> Unit * @see RouterFunctions.nest */ fun RequestPredicate.nest(r: (CoRouterFunctionDsl.() -> Unit)) { - builder.add(nest(this, CoRouterFunctionDsl(r).invoke())) + builder.add(nest(this, CoRouterFunctionDsl(r).build())) } @@ -396,10 +405,10 @@ open class CoRouterFunctionDsl(private val init: (CoRouterFunctionDsl.() -> Unit } } - /** - * Return a composed routing function created from all the registered routes. - */ - override fun invoke(): RouterFunction { + /** + * Return a composed routing function created from all the registered routes. + */ + internal fun build(): RouterFunction { init() return builder.build() } @@ -484,12 +493,3 @@ open class CoRouterFunctionDsl(private val init: (CoRouterFunctionDsl.() -> Unit */ operator fun RouterFunction.plus(other: RouterFunction) = this.and(other) - -/** - * Coroutines variant of [router]. - * - * @author Sebastien Deleuze - * @since 5.2 - */ -fun coRouter(routes: (CoRouterFunctionDsl.() -> Unit)) = - CoRouterFunctionDsl(routes).invoke() \ No newline at end of file diff --git a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDsl.kt b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDsl.kt index f2b5b7ba66..e85715c010 100644 --- a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDsl.kt +++ b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDsl.kt @@ -26,8 +26,8 @@ import java.net.URI import java.util.function.Supplier /** - * Allow to create easily a `RouterFunction` from a Kotlin router DSL based - * on the same building blocks as the Java one ([RouterFunction], [RequestPredicate], + * Allow to create easily a WebFlux.fn `RouterFunction` from a Kotlin + * router DSL leveraging WebFlux.fn Java API ([RouterFunction], [RequestPredicate], * [HandlerFunction]). * * Example: @@ -55,7 +55,7 @@ import java.util.function.Supplier * @see RouterFunctions.Builder * @since 5.0 */ -fun router(routes: RouterFunctionDsl.() -> Unit) = RouterFunctionDsl(routes).invoke() +fun router(routes: RouterFunctionDsl.() -> Unit) = RouterFunctionDsl(routes).build() /** * Provide a [RouterFunction] Kotlin DSL in order to be able to write idiomatic Kotlin code. @@ -64,7 +64,7 @@ fun router(routes: RouterFunctionDsl.() -> Unit) = RouterFunctionDsl(routes).inv * @author Yevhenii Melnyk * @since 5.0 */ -open class RouterFunctionDsl(private val init: RouterFunctionDsl.() -> Unit) : () -> RouterFunction { +class RouterFunctionDsl(private val init: RouterFunctionDsl.() -> Unit) { private val builder = RouterFunctions.route() @@ -136,7 +136,7 @@ open class RouterFunctionDsl(private val init: RouterFunctionDsl.() -> Unit) : ( * @see RouterFunctions.nest */ fun RequestPredicate.nest(init: RouterFunctionDsl.() -> Unit) { - builder.nest(this, Supplier { RouterFunctionDsl(init).invoke() }) + builder.nest(this, Supplier { RouterFunctionDsl(init).build() }) } /** @@ -148,7 +148,7 @@ open class RouterFunctionDsl(private val init: RouterFunctionDsl.() -> Unit) : ( * @see RequestPredicates.path */ fun String.nest(init: RouterFunctionDsl.() -> Unit) { - builder.path(this, Supplier { RouterFunctionDsl(init).invoke() }) + builder.path(this, Supplier { RouterFunctionDsl(init).build() }) } /** @@ -545,7 +545,7 @@ open class RouterFunctionDsl(private val init: RouterFunctionDsl.() -> Unit) : ( * Return a composed routing function created from all the registered routes. * @since 5.1 */ - override fun invoke(): RouterFunction { + internal fun build(): RouterFunction { init() return builder.build() }