Browse Source

Fix pathExtension null-safety in Kotlin DSLs

Closes gh-32254
pull/32259/head
Sébastien Deleuze 2 years ago
parent
commit
2fe3321813
  1. 4
      spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDsl.kt
  2. 4
      spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDsl.kt
  3. 12
      spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/CoRouterFunctionDslTests.kt
  4. 12
      spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionDslTests.kt
  5. 4
      spring-webmvc/src/main/kotlin/org/springframework/web/servlet/function/RouterFunctionDsl.kt
  6. 10
      spring-webmvc/src/test/kotlin/org/springframework/web/servlet/function/RouterFunctionDslTests.kt

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

@ -452,7 +452,7 @@ class CoRouterFunctionDsl internal constructor (private val init: (CoRouterFunct @@ -452,7 +452,7 @@ class CoRouterFunctionDsl internal constructor (private val init: (CoRouterFunct
* Route to the given handler function if the given pathExtension predicate applies.
* @see RouterFunctions.route
*/
fun pathExtension(predicate: (String) -> Boolean, f: suspend (ServerRequest) -> ServerResponse) {
fun pathExtension(predicate: (String?) -> Boolean, f: suspend (ServerRequest) -> ServerResponse) {
builder.add(RouterFunctions.route(RequestPredicates.pathExtension(predicate), asHandlerFunction(f)))
}
@ -461,7 +461,7 @@ class CoRouterFunctionDsl internal constructor (private val init: (CoRouterFunct @@ -461,7 +461,7 @@ class CoRouterFunctionDsl internal constructor (private val init: (CoRouterFunct
* predicate.
* @see RequestPredicates.pathExtension
*/
fun pathExtension(predicate: (String) -> Boolean): RequestPredicate =
fun pathExtension(predicate: (String?) -> Boolean): RequestPredicate =
RequestPredicates.pathExtension(predicate)
/**

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

@ -569,7 +569,7 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs @@ -569,7 +569,7 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs
* Route to the given handler function if the given pathExtension predicate applies.
* @see RouterFunctions.route
*/
fun pathExtension(predicate: (String) -> Boolean, f: (ServerRequest) -> Mono<out ServerResponse>) {
fun pathExtension(predicate: (String?) -> Boolean, f: (ServerRequest) -> Mono<out ServerResponse>) {
builder.add(RouterFunctions.route(RequestPredicates.pathExtension(predicate), HandlerFunction { f(it).cast(ServerResponse::class.java) }))
}
@ -578,7 +578,7 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs @@ -578,7 +578,7 @@ class RouterFunctionDsl internal constructor (private val init: RouterFunctionDs
* predicate.
* @see RequestPredicates.pathExtension
*/
fun pathExtension(predicate: (String) -> Boolean): RequestPredicate =
fun pathExtension(predicate: (String?) -> Boolean): RequestPredicate =
RequestPredicates.pathExtension(predicate)
/**

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

@ -112,6 +112,15 @@ class CoRouterFunctionDslTests { @@ -112,6 +112,15 @@ class CoRouterFunctionDslTests {
.verifyComplete()
}
@Test
fun pathExtension() {
val mockRequest = get("https://example.com/test.properties").build()
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
StepVerifier.create(sampleRouter().route(request))
.expectNextCount(1)
.verifyComplete()
}
@Test
fun resource() {
val mockRequest = get("https://example.com/response2.txt").build()
@ -324,6 +333,9 @@ class CoRouterFunctionDslTests { @@ -324,6 +333,9 @@ class CoRouterFunctionDslTests {
null
}
}
GET("/**", pathExtension { it == "properties" }) {
ok().bodyValueAndAwait("foo=bar")
}
path("/baz", ::handle)
GET("/rendering") { RenderingResponse.create("index").buildAndAwait() }
add(otherRouter)

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

@ -110,6 +110,15 @@ class RouterFunctionDslTests { @@ -110,6 +110,15 @@ class RouterFunctionDslTests {
.verifyComplete()
}
@Test
fun pathExtension() {
val mockRequest = get("https://example.com/test.properties").build()
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
StepVerifier.create(sampleRouter().route(request))
.expectNextCount(1)
.verifyComplete()
}
@Test
fun resource() {
val mockRequest = get("https://example.com/response2.txt").build()
@ -256,6 +265,9 @@ class RouterFunctionDslTests { @@ -256,6 +265,9 @@ class RouterFunctionDslTests {
Mono.empty()
}
}
GET(pathExtension { it == "properties" }) {
ok().bodyValue("foo=bar")
}
path("/baz", ::handle)
GET("/rendering") { RenderingResponse.create("index").build() }
add(otherRouter)

4
spring-webmvc/src/main/kotlin/org/springframework/web/servlet/function/RouterFunctionDsl.kt

@ -564,7 +564,7 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD @@ -564,7 +564,7 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD
* Route to the given handler function if the given pathExtension predicate applies.
* @see RouterFunctions.route
*/
fun pathExtension(predicate: (String) -> Boolean, f: (ServerRequest) -> ServerResponse) {
fun pathExtension(predicate: (String?) -> Boolean, f: (ServerRequest) -> ServerResponse) {
builder.add(RouterFunctions.route(RequestPredicates.pathExtension(predicate), HandlerFunction(f)))
}
@ -573,7 +573,7 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD @@ -573,7 +573,7 @@ class RouterFunctionDsl internal constructor (private val init: (RouterFunctionD
* predicate.
* @see RequestPredicates.pathExtension
*/
fun pathExtension(predicate: (String) -> Boolean): RequestPredicate =
fun pathExtension(predicate: (String?) -> Boolean): RequestPredicate =
RequestPredicates.pathExtension(predicate)
/**

10
spring-webmvc/src/test/kotlin/org/springframework/web/servlet/function/RouterFunctionDslTests.kt

@ -88,6 +88,13 @@ class RouterFunctionDslTests { @@ -88,6 +88,13 @@ class RouterFunctionDslTests {
assertThat(sampleRouter().route(request).isPresent).isTrue()
}
@Test
fun pathExtension() {
val servletRequest = PathPatternsTestUtils.initRequest("GET", "/test.properties", true)
val request = DefaultServerRequest(servletRequest, emptyList())
assertThat(sampleRouter().route(request).isPresent).isTrue()
}
@Test
fun resource() {
val servletRequest = PathPatternsTestUtils.initRequest("GET","/response2.txt", true)
@ -185,6 +192,9 @@ class RouterFunctionDslTests { @@ -185,6 +192,9 @@ class RouterFunctionDslTests {
null
}
}
GET(pathExtension { it == "properties" }) {
ok().body("foo=bar")
}
path("/baz", ::handle)
GET("/rendering") { RenderingResponse.create("index").build() }
add(otherRouter)

Loading…
Cancel
Save