Fix pathExtension null-safety in Kotlin DSLs

Closes gh-32254
This commit is contained in:
Sébastien Deleuze 2024-02-13 14:24:19 +01:00
parent 8c3fc8c549
commit 2fe3321813
6 changed files with 40 additions and 6 deletions

View File

@ -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
* predicate.
* @see RequestPredicates.pathExtension
*/
fun pathExtension(predicate: (String) -> Boolean): RequestPredicate =
fun pathExtension(predicate: (String?) -> Boolean): RequestPredicate =
RequestPredicates.pathExtension(predicate)
/**

View File

@ -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
* predicate.
* @see RequestPredicates.pathExtension
*/
fun pathExtension(predicate: (String) -> Boolean): RequestPredicate =
fun pathExtension(predicate: (String?) -> Boolean): RequestPredicate =
RequestPredicates.pathExtension(predicate)
/**

View File

@ -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 {
null
}
}
GET("/**", pathExtension { it == "properties" }) {
ok().bodyValueAndAwait("foo=bar")
}
path("/baz", ::handle)
GET("/rendering") { RenderingResponse.create("index").buildAndAwait() }
add(otherRouter)

View File

@ -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 {
Mono.empty()
}
}
GET(pathExtension { it == "properties" }) {
ok().bodyValue("foo=bar")
}
path("/baz", ::handle)
GET("/rendering") { RenderingResponse.create("index").build() }
add(otherRouter)

View File

@ -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
* predicate.
* @see RequestPredicates.pathExtension
*/
fun pathExtension(predicate: (String) -> Boolean): RequestPredicate =
fun pathExtension(predicate: (String?) -> Boolean): RequestPredicate =
RequestPredicates.pathExtension(predicate)
/**

View File

@ -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 {
null
}
}
GET(pathExtension { it == "properties" }) {
ok().body("foo=bar")
}
path("/baz", ::handle)
GET("/rendering") { RenderingResponse.create("index").build() }
add(otherRouter)