Refine WebFlux Coroutines API
This commit provides both nullable and non-nullable variants for awaitBody, makes awaitPrincipal return type nullable and rename awaitResponse to awaitExchange for better consistency with Java API. See gh-19975
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.web.reactive.function.client
|
||||
|
||||
import kotlinx.coroutines.reactive.awaitFirstOrNull
|
||||
import kotlinx.coroutines.reactive.awaitSingle
|
||||
import org.springframework.core.ParameterizedTypeReference
|
||||
import org.springframework.http.ResponseEntity
|
||||
@@ -67,7 +68,7 @@ inline fun <reified T : Any> ClientResponse.toEntityList(): Mono<ResponseEntity<
|
||||
toEntityList(object : ParameterizedTypeReference<T>() {})
|
||||
|
||||
/**
|
||||
* Coroutines variant of [ClientResponse.bodyToMono].
|
||||
* Non-nullable Coroutines variant of [ClientResponse.bodyToMono].
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.2
|
||||
@@ -75,6 +76,15 @@ inline fun <reified T : Any> ClientResponse.toEntityList(): Mono<ResponseEntity<
|
||||
suspend inline fun <reified T : Any> ClientResponse.awaitBody(): T =
|
||||
bodyToMono<T>().awaitSingle()
|
||||
|
||||
/**
|
||||
* Nullable coroutines variant of [ClientResponse.bodyToMono].
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.2
|
||||
*/
|
||||
suspend inline fun <reified T : Any> ClientResponse.awaitBodyOrNull(): T? =
|
||||
bodyToMono<T>().awaitFirstOrNull()
|
||||
|
||||
/**
|
||||
* Coroutines variant of [ClientResponse.toEntity].
|
||||
*
|
||||
|
||||
@@ -67,7 +67,7 @@ inline fun <reified T : Any> WebClient.ResponseSpec.bodyToFlux(): Flux<T> =
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.2
|
||||
*/
|
||||
suspend fun WebClient.RequestHeadersSpec<out WebClient.RequestHeadersSpec<*>>.awaitResponse(): ClientResponse =
|
||||
suspend fun WebClient.RequestHeadersSpec<out WebClient.RequestHeadersSpec<*>>.awaitExchange(): ClientResponse =
|
||||
exchange().awaitSingle()
|
||||
|
||||
/**
|
||||
|
||||
@@ -49,12 +49,21 @@ inline fun <reified T : Any> ServerRequest.bodyToFlux(): Flux<T> =
|
||||
bodyToFlux(object : ParameterizedTypeReference<T>() {})
|
||||
|
||||
/**
|
||||
* Coroutines variant of [ServerRequest.bodyToMono].
|
||||
* Non-nullable Coroutines variant of [ServerRequest.bodyToMono].
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.2
|
||||
*/
|
||||
suspend inline fun <reified T : Any> ServerRequest.awaitBody(): T? =
|
||||
suspend inline fun <reified T : Any> ServerRequest.awaitBody(): T =
|
||||
bodyToMono<T>().awaitSingle()
|
||||
|
||||
/**
|
||||
* Nullable Coroutines variant of [ServerRequest.bodyToMono].
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.2
|
||||
*/
|
||||
suspend inline fun <reified T : Any> ServerRequest.awaitBodyOrNull(): T? =
|
||||
bodyToMono<T>().awaitFirstOrNull()
|
||||
|
||||
/**
|
||||
@@ -81,8 +90,8 @@ suspend fun ServerRequest.awaitMultipartData(): MultiValueMap<String, Part> =
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.2
|
||||
*/
|
||||
suspend fun ServerRequest.awaitPrincipal(): Principal =
|
||||
principal().awaitSingle()
|
||||
suspend fun ServerRequest.awaitPrincipal(): Principal? =
|
||||
principal().awaitFirstOrNull()
|
||||
|
||||
/**
|
||||
* Coroutines variant of [ServerRequest.session].
|
||||
|
||||
@@ -21,6 +21,7 @@ import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Test
|
||||
import org.springframework.core.ParameterizedTypeReference
|
||||
import org.springframework.http.HttpStatus
|
||||
@@ -69,6 +70,15 @@ class ClientResponseExtensionsTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun awaitBodyOrNull() {
|
||||
val response = mockk<ClientResponse>()
|
||||
every { response.bodyToMono<String>() } returns Mono.empty()
|
||||
runBlocking {
|
||||
assertNull(response.awaitBodyOrNull<String>())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun awaitEntity() {
|
||||
val response = mockk<ClientResponse>()
|
||||
|
||||
@@ -58,11 +58,11 @@ class WebClientExtensionsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun awaitResponse() {
|
||||
fun awaitExchange() {
|
||||
val response = mockk<ClientResponse>()
|
||||
every { requestBodySpec.exchange() } returns Mono.just(response)
|
||||
runBlocking {
|
||||
assertEquals(response, requestBodySpec.awaitResponse())
|
||||
assertEquals(response, requestBodySpec.awaitExchange())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -61,10 +61,10 @@ class ServerRequestExtensionsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun awaitBodyNull() {
|
||||
fun awaitBodyOrNull() {
|
||||
every { request.bodyToMono<String>() } returns Mono.empty()
|
||||
runBlocking {
|
||||
assertNull(request.awaitBody<String>())
|
||||
assertNull(request.awaitBodyOrNull<String>())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user