From 03f34e24ca588e6f1bdf20287d43c958b952b724 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Thu, 16 Feb 2017 17:18:50 +0100 Subject: [PATCH] Add nested route support to Kotlin DSL Issue: SPR-14954 --- .../function/server/RouterFunctionExtensions.kt | 3 +++ .../function/server/RouterFunctionExtensionsTests.kt | 10 +++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt index d2882b83fb7..dd7ddfb9a95 100644 --- a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt +++ b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt @@ -69,6 +69,9 @@ class RouterDsl { operator fun RequestPredicate.not(): RequestPredicate = this.negate() + fun RequestPredicate.route(routes: RouterDsl.() -> Unit) = + RouterFunctions.nest(this, RouterDsl().apply(routes).router()) + operator fun RequestPredicate.invoke(f: (ServerRequest) -> Mono) { routes += RouterFunctions.route(this, HandlerFunction { f(it) }) } diff --git a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt index a71045a6112..3750bc4fb75 100644 --- a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt +++ b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt @@ -112,15 +112,15 @@ class RouterFunctionExtensionsTests { override fun route(req: ServerRequest) = route(req) { (GET("/foo/") or GET("/foos/")) { handle(req) } - accept(APPLICATION_JSON).apply { - POST("/api/foo/") { handleFromClass(req) } - PUT("/api/foo/") { handleFromClass(req) } - DELETE("/api/foo/") { handleFromClass(req) } + (pathPrefix("/api") and accept(APPLICATION_JSON)).route { + POST("/foo/") { handleFromClass(req) } + PUT("/foo/") { handleFromClass(req) } + DELETE("/foo/") { handleFromClass(req) } } accept(APPLICATION_ATOM_XML, ::handle) contentType(APPLICATION_OCTET_STREAM) { handle(req) } method(HttpMethod.PATCH) { handle(req) } - headers({ it.accept().contains(APPLICATION_JSON) }).apply { + headers({ it.accept().contains(APPLICATION_JSON) }).route { GET("/api/foo/", ::handle) } headers({ it.header("bar").isNotEmpty() }, ::handle)