Skip to content

Add ServerRequest.pathVariableOrNull Kotlin extension #32738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
*/
fun ServerRequest.pathVariableOrNull(name: String): String? {
val vars = pathVariables()
return vars[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
}