Browse Source

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
pull/22705/head
Sebastien Deleuze 6 years ago
parent
commit
c5c4ac164b
  1. 12
      spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/client/ClientResponseExtensions.kt
  2. 2
      spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/client/WebClientExtensions.kt
  3. 17
      spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtensions.kt
  4. 10
      spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/ClientResponseExtensionsTests.kt
  5. 4
      spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/WebClientExtensionsTests.kt
  6. 4
      spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtensionsTests.kt

12
spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/client/ClientResponseExtensions.kt

@ -16,6 +16,7 @@ @@ -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< @@ -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< @@ -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].
*

2
spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/client/WebClientExtensions.kt

@ -67,7 +67,7 @@ inline fun <reified T : Any> WebClient.ResponseSpec.bodyToFlux(): Flux<T> = @@ -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()
/**

17
spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtensions.kt

@ -49,12 +49,21 @@ inline fun <reified T : Any> ServerRequest.bodyToFlux(): Flux<T> = @@ -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> = @@ -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].

10
spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/ClientResponseExtensionsTests.kt

@ -21,6 +21,7 @@ import io.mockk.mockk @@ -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 { @@ -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>()

4
spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/WebClientExtensionsTests.kt

@ -58,11 +58,11 @@ class WebClientExtensionsTests { @@ -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())
}
}

4
spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtensionsTests.kt

@ -61,10 +61,10 @@ class ServerRequestExtensionsTests { @@ -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>())
}
}

Loading…
Cancel
Save