Skip to content

Commit 478aa25

Browse files
ilya40umovsdeleuze
authored andcommitted
Preserve coroutine context in WebClientExtensions
See gh-33548
1 parent 5cc4d0a commit 478aa25

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.web.reactive.function.client
1818

19+
import kotlinx.coroutines.Job
20+
import kotlinx.coroutines.currentCoroutineContext
1921
import kotlinx.coroutines.Dispatchers
2022
import kotlinx.coroutines.flow.Flow
2123
import kotlinx.coroutines.reactive.asFlow
@@ -87,16 +89,20 @@ suspend fun RequestHeadersSpec<out RequestHeadersSpec<*>>.awaitExchange(): Clien
8789
* @author Sebastien Deleuze
8890
* @since 5.3
8991
*/
90-
suspend fun <T: Any> RequestHeadersSpec<out RequestHeadersSpec<*>>.awaitExchange(responseHandler: suspend (ClientResponse) -> T): T =
91-
exchangeToMono { mono(Dispatchers.Unconfined) { responseHandler.invoke(it) } }.awaitSingle()
92+
suspend fun <T: Any> RequestHeadersSpec<out RequestHeadersSpec<*>>.awaitExchange(responseHandler: suspend (ClientResponse) -> T): T {
93+
val context = currentCoroutineContext().minusKey(Job.Key)
94+
return exchangeToMono { mono(context) { responseHandler.invoke(it) } }.awaitSingle()
95+
}
9296

9397
/**
9498
* Variant of [WebClient.RequestHeadersSpec.awaitExchange] that allows a nullable return
9599
*
96100
* @since 5.3.8
97101
*/
98-
suspend fun <T: Any> RequestHeadersSpec<out RequestHeadersSpec<*>>.awaitExchangeOrNull(responseHandler: suspend (ClientResponse) -> T?): T? =
99-
exchangeToMono { mono(Dispatchers.Unconfined) { responseHandler.invoke(it) } }.awaitSingleOrNull()
102+
suspend fun <T: Any> RequestHeadersSpec<out RequestHeadersSpec<*>>.awaitExchangeOrNull(responseHandler: suspend (ClientResponse) -> T?): T? {
103+
val context = currentCoroutineContext().minusKey(Job.Key)
104+
return exchangeToMono { mono(context) { responseHandler.invoke(it) } }.awaitSingleOrNull()
105+
}
100106

101107
/**
102108
* Coroutines variant of [WebClient.RequestHeadersSpec.exchangeToFlux].

0 commit comments

Comments
 (0)