Skip to content

Commit

Permalink
Add ServerRequest.pathVariableOrNull Kotlin extension
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgePap-719 authored and sdeleuze committed May 2, 2024
1 parent 02b3801 commit 3f3995f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 3f3995f

Please sign in to comment.