Skip to content

Commit 03f34e2

Browse files
committed
Add nested route support to Kotlin DSL
Issue: SPR-14954
1 parent 0da8dee commit 03f34e2

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class RouterDsl {
6969

7070
operator fun RequestPredicate.not(): RequestPredicate = this.negate()
7171

72+
fun RequestPredicate.route(routes: RouterDsl.() -> Unit) =
73+
RouterFunctions.nest(this, RouterDsl().apply(routes).router())
74+
7275
operator fun RequestPredicate.invoke(f: (ServerRequest) -> Mono<ServerResponse>) {
7376
routes += RouterFunctions.route(this, HandlerFunction { f(it) })
7477
}

spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ class RouterFunctionExtensionsTests {
112112

113113
override fun route(req: ServerRequest) = route(req) {
114114
(GET("/foo/") or GET("/foos/")) { handle(req) }
115-
accept(APPLICATION_JSON).apply {
116-
POST("/api/foo/") { handleFromClass(req) }
117-
PUT("/api/foo/") { handleFromClass(req) }
118-
DELETE("/api/foo/") { handleFromClass(req) }
115+
(pathPrefix("/api") and accept(APPLICATION_JSON)).route {
116+
POST("/foo/") { handleFromClass(req) }
117+
PUT("/foo/") { handleFromClass(req) }
118+
DELETE("/foo/") { handleFromClass(req) }
119119
}
120120
accept(APPLICATION_ATOM_XML, ::handle)
121121
contentType(APPLICATION_OCTET_STREAM) { handle(req) }
122122
method(HttpMethod.PATCH) { handle(req) }
123-
headers({ it.accept().contains(APPLICATION_JSON) }).apply {
123+
headers({ it.accept().contains(APPLICATION_JSON) }).route {
124124
GET("/api/foo/", ::handle)
125125
}
126126
headers({ it.header("bar").isNotEmpty() }, ::handle)

0 commit comments

Comments
 (0)