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:
Sebastien Deleuze
2019-03-28 10:17:12 +01:00
parent 811f315440
commit c5c4ac164b
6 changed files with 39 additions and 10 deletions

View File

@@ -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].
*

View File

@@ -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()
/**

View File

@@ -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].

View File

@@ -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>()

View File

@@ -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())
}
}

View File

@@ -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>())
}
}