From a662f5e71932ea08f3f384a80ae01964185fc640 Mon Sep 17 00:00:00 2001 From: donghui Date: Wed, 24 Apr 2024 23:20:25 +0900 Subject: [PATCH] Add a requiredBody() extension to RestClient.ResponseSpec Closes gh-32703 --- .../web/client/RestClientExtensions.kt | 12 ++++++++++-- .../web/client/RestClientExtensionsTests.kt | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/spring-web/src/main/kotlin/org/springframework/web/client/RestClientExtensions.kt b/spring-web/src/main/kotlin/org/springframework/web/client/RestClientExtensions.kt index 9ed98fcad6e4..12092af8dfea 100644 --- a/spring-web/src/main/kotlin/org/springframework/web/client/RestClientExtensions.kt +++ b/spring-web/src/main/kotlin/org/springframework/web/client/RestClientExtensions.kt @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,6 +42,14 @@ inline fun RestClient.RequestBodySpec.bodyWithType(body: T): R inline fun RestClient.ResponseSpec.body(): T? = body(object : ParameterizedTypeReference() {}) +/** + * Extension for [RestClient.ResponseSpec.body] providing a `requiredBody()` variant with a non-nullable + * return value. + * @throws NoSuchElementException if there is no response body + * @since 6.2 + */ +inline fun RestClient.ResponseSpec.requiredBody(): T = + body(object : ParameterizedTypeReference() {}) ?: throw NoSuchElementException("Response body is required") /** * Extension for [RestClient.ResponseSpec.toEntity] providing a `toEntity()` variant @@ -52,4 +60,4 @@ inline fun RestClient.ResponseSpec.body(): T? = * @since 6.1 */ inline fun RestClient.ResponseSpec.toEntity(): ResponseEntity = - toEntity(object : ParameterizedTypeReference() {}) + toEntity(object : ParameterizedTypeReference() {}) \ No newline at end of file diff --git a/spring-web/src/test/kotlin/org/springframework/web/client/RestClientExtensionsTests.kt b/spring-web/src/test/kotlin/org/springframework/web/client/RestClientExtensionsTests.kt index 8b68868f3281..6e915901664b 100644 --- a/spring-web/src/test/kotlin/org/springframework/web/client/RestClientExtensionsTests.kt +++ b/spring-web/src/test/kotlin/org/springframework/web/client/RestClientExtensionsTests.kt @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,9 +16,11 @@ package org.springframework.web.client +import io.mockk.every import io.mockk.mockk import io.mockk.verify import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows import org.springframework.core.ParameterizedTypeReference /** @@ -45,6 +47,18 @@ class RestClientExtensionsTests { verify { responseSpec.body(object : ParameterizedTypeReference>() {}) } } + @Test + fun `ResponseSpec#requiredBody with reified type parameters`() { + responseSpec.requiredBody>() + verify { responseSpec.body(object : ParameterizedTypeReference>() {}) } + } + + @Test + fun `ResponseSpec#requiredBody with null response throws NoSuchElementException`() { + every { responseSpec.body(any>()) } returns null + assertThrows { responseSpec.requiredBody() } + } + @Test fun `ResponseSpec#toEntity with reified type parameters`() { responseSpec.toEntity>()