Add ServerRequest.pathVariableOrNull Kotlin extension
Closes gh-32738
This commit is contained in:
committed by
Sébastien Deleuze
parent
02b3801f1a
commit
3f3995f846
@@ -35,7 +35,7 @@ import kotlin.reflect.KClass
|
||||
* Extension for [ServerRequest.bodyToMono] providing a `bodyToMono<Foo>()` variant
|
||||
* leveraging Kotlin reified type parameters. This extension is not subject to type
|
||||
* erasure and retains actual generic type arguments.
|
||||
*
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
@@ -192,3 +192,13 @@ fun ServerRequest.Headers.contentLengthOrNull(): Long? =
|
||||
*/
|
||||
fun ServerRequest.Headers.contentTypeOrNull(): MediaType? =
|
||||
contentType().orElse(null)
|
||||
|
||||
/**
|
||||
* Nullable variant of [ServerRequest.pathVariable].
|
||||
*
|
||||
* @author George Papadopoulos
|
||||
* @since 6.2
|
||||
*/
|
||||
fun ServerRequest.pathVariableOrNull(name: String): String? {
|
||||
return pathVariables()[name]
|
||||
}
|
||||
|
||||
@@ -222,5 +222,19 @@ class ServerRequestExtensionsTests {
|
||||
verify { headers.contentType() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `pathVariableOrNull with value`() {
|
||||
every { request.pathVariables() } returns hashMapOf("name" to "123")
|
||||
assert(request.pathVariableOrNull("name") == "123")
|
||||
verify { request.pathVariables() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `pathVariableOrNull with null`() {
|
||||
every { request.pathVariables() } returns emptyMap()
|
||||
assert(request.pathVariableOrNull("name") == null)
|
||||
verify { request.pathVariables() }
|
||||
}
|
||||
|
||||
class Foo
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user