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
This commit is contained in:
Sebastien Deleuze 2019-04-04 11:56:20 +02:00
parent e6171fb47d
commit 2eb631aaad
2 changed files with 22 additions and 22 deletions

View File

@ -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<ServerResponse> {
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<ServerResponse> {
/**
* Return a composed routing function created from all the registered routes.
*/
internal fun build(): RouterFunction<ServerResponse> {
init()
return builder.build()
}
@ -484,12 +493,3 @@ open class CoRouterFunctionDsl(private val init: (CoRouterFunctionDsl.() -> Unit
*/
operator fun <T: ServerResponse> RouterFunction<T>.plus(other: RouterFunction<T>) =
this.and(other)
/**
* Coroutines variant of [router].
*
* @author Sebastien Deleuze
* @since 5.2
*/
fun coRouter(routes: (CoRouterFunctionDsl.() -> Unit)) =
CoRouterFunctionDsl(routes).invoke()

View File

@ -26,8 +26,8 @@ import java.net.URI
import java.util.function.Supplier
/**
* Allow to create easily a `RouterFunction<ServerResponse>` 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<ServerResponse>` 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<ServerResponse> {
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<ServerResponse> {
internal fun build(): RouterFunction<ServerResponse> {
init()
return builder.build()
}