Skip to content

Commit 3530443

Browse files
committed
Add RestClient Kotlin extensions
Closes gh-30807
1 parent 4202553 commit 3530443

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.client
18+
19+
import org.springframework.core.ParameterizedTypeReference
20+
import org.springframework.http.ResponseEntity
21+
22+
/**
23+
* Extension for [RestClient.RequestBodySpec.body] providing a `bodyWithType<Foo>(...)` variant
24+
* leveraging Kotlin reified type parameters. This extension is not subject to type
25+
* erasure and retains actual generic type arguments.
26+
*
27+
* @author Sebastien Deleuze
28+
* @since 6.1
29+
*/
30+
inline fun <reified T : Any> RestClient.RequestBodySpec.bodyWithType(body: T): RestClient.RequestBodySpec =
31+
body(body, object : ParameterizedTypeReference<T>() {})
32+
33+
34+
/**
35+
* Extension for [RestClient.ResponseSpec.body] providing a `body<Foo>()` variant
36+
* leveraging Kotlin reified type parameters. This extension is not subject to type
37+
* erasure and retains actual generic type arguments.
38+
*
39+
* @author Sebastien Deleuze
40+
* @since 6.1
41+
*/
42+
inline fun <reified T : Any> RestClient.ResponseSpec.body(): T? =
43+
body(object : ParameterizedTypeReference<T>() {})
44+
45+
46+
/**
47+
* Extension for [RestClient.ResponseSpec.toEntity] providing a `toEntity<Foo>()` variant
48+
* leveraging Kotlin reified type parameters. This extension is not subject to type
49+
* erasure and retains actual generic type arguments.
50+
*
51+
* @author Sebastien Deleuze
52+
* @since 6.1
53+
*/
54+
inline fun <reified T : Any> RestClient.ResponseSpec.toEntity(): ResponseEntity<T> =
55+
toEntity(object : ParameterizedTypeReference<T>() {})
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.client
18+
19+
import io.mockk.mockk
20+
import io.mockk.verify
21+
import org.junit.jupiter.api.Test
22+
import org.springframework.core.ParameterizedTypeReference
23+
24+
/**
25+
* Mock object based tests for [RestClient] Kotlin extensions
26+
*
27+
* @author Sebastien Deleuze
28+
*/
29+
class RestClientExtensionsTests {
30+
31+
private val requestBodySpec = mockk<RestClient.RequestBodySpec>(relaxed = true)
32+
33+
private val responseSpec = mockk<RestClient.ResponseSpec>(relaxed = true)
34+
35+
@Test
36+
fun `RequestBodySpec#body with reified type parameters`() {
37+
val body = mockk<List<Foo>>()
38+
requestBodySpec.bodyWithType(body)
39+
verify { requestBodySpec.body(body, object : ParameterizedTypeReference<List<Foo>>() {}) }
40+
}
41+
42+
@Test
43+
fun `ResponseSpec#body with reified type parameters`() {
44+
responseSpec.body<List<Foo>>()
45+
verify { responseSpec.body(object : ParameterizedTypeReference<List<Foo>>() {}) }
46+
}
47+
48+
@Test
49+
fun `ResponseSpec#toEntity with reified type parameters`() {
50+
responseSpec.toEntity<List<Foo>>()
51+
verify { responseSpec.toEntity(object : ParameterizedTypeReference<List<Foo>>() {}) }
52+
}
53+
54+
private class Foo
55+
56+
}

0 commit comments

Comments
 (0)