Skip to content
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

Fall back on awaitToBodylessEntity when awaitBody is used with Unit #26504

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,16 @@ inline fun <reified T : Any> WebClient.ResponseSpec.bodyToFlow(): Flow<T> =
* @since 5.2
*/
suspend inline fun <reified T : Any> WebClient.ResponseSpec.awaitBody() : T =
bodyToMono<T>().awaitSingle()
when (T::class) {
Unit::class -> awaitBodilessEntity().let { Unit as T }
else -> bodyToMono<T>().awaitSingle()
}

/**
* Coroutines variant of [WebClient.ResponseSpec.toBodilessEntity].
*/
suspend inline fun WebClient.ResponseSpec.awaitBodilessEntity() =
toBodilessEntity().awaitSingle()

/**
* Extension for [WebClient.ResponseSpec.toEntity] providing a `toEntity<Foo>()` variant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ class WebClientExtensionsTests {
}
}

@Test
fun `awaitBody of type Unit`() {
val spec = mockk<WebClient.ResponseSpec>()
every { spec.toBodilessEntity() } returns Mono.empty()
runBlocking {
assertThat(spec.awaitBody<Unit>()).isEqualTo(Unit)
}
}

@Test
fun awaitBodilessEntity() {
val spec = mockk<WebClient.ResponseSpec>()
every { spec.toBodilessEntity() } returns Mono.empty()
runBlocking {
assertThat(spec.awaitBodilessEntity()).isEqualTo(Unit)
}
}

@Test
fun `ResponseSpec#toEntity with reified type parameters`() {
responseSpec.toEntity<List<Foo>>()
Expand Down