Skip to content

Commit fce1dbf

Browse files
committed
Add Kotlin extension functions for ClientResponseField
In gh-1331, several Kotlin extensions functions were added for `GraphQlClient` specs. This commit adds missing extension functions for `ClientResponseField`. Closes gh-1393
1 parent c0abfa8 commit fce1dbf

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2025-present 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.graphql.client
18+
19+
import org.springframework.core.ParameterizedTypeReference
20+
21+
/**
22+
* Extension for [ClientResponseField.toEntity] providing a `toEntity<Book>()` variant
23+
* leveraging Kotlin reified type parameters. This extension is not subject to type
24+
* erasure and retains actual generic type arguments.
25+
*
26+
* @author Brian Clozel
27+
* @since 2.0.1
28+
*/
29+
inline fun <reified T : Any> ClientResponseField.toEntity(): T? =
30+
toEntity(object : ParameterizedTypeReference<T>() {})
31+
32+
/**
33+
* Extension for [ClientResponseField.toEntityList] providing a `toEntityList<Book>()` variant
34+
* leveraging Kotlin reified type parameters. This extension is not subject to type
35+
* erasure and retains actual generic type arguments.
36+
*
37+
* @author Brian Clozel
38+
* @since 2.0.1
39+
*/
40+
inline fun <reified T : Any> ClientResponseField.toEntityList(): List<T> =
41+
toEntityList(object : ParameterizedTypeReference<T>() {})

spring-graphql/src/main/kotlin/org/springframework/graphql/client/GraphQlClientExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@ inline fun <reified T : Any> GraphQlClient.RetrieveSubscriptionSpec.toEntity():
8484
* @since 2.0.0
8585
*/
8686
inline fun <reified T : Any> GraphQlClient.RetrieveSubscriptionSpec.toEntityList(): Flux<List<T>> =
87-
toEntityList(object : ParameterizedTypeReference<T>() {})
87+
toEntityList(object : ParameterizedTypeReference<T>() {})

spring-graphql/src/test/kotlin/org/springframework/graphql/client/GraphQlClientExtensionsTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@ class GraphQlClientExtensionsTests {
7575

7676
private class Book
7777

78-
}
78+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2025-present 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.graphql.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+
/**
26+
* Tests for [ClientResponseField] Kotlin extensions.
27+
*
28+
* @author Brian Clozel
29+
*/
30+
class GraphQlResponseFieldExtensionsTests {
31+
32+
private val responseField = mockk<ClientResponseField>(relaxed = true)
33+
34+
@Test
35+
fun `ClientResponseField#toEntity with reified type parameters`() {
36+
responseField.toEntity<Map<String, Book>>()
37+
verify { responseField.toEntity(object : ParameterizedTypeReference<Map<String, Book>>() {}) }
38+
}
39+
40+
@Test
41+
fun `ClientResponseField#toEntityList with reified type parameters`() {
42+
responseField.toEntityList<Book>()
43+
verify { responseField.toEntityList(object : ParameterizedTypeReference<Book>() {}) }
44+
}
45+
46+
private class Book
47+
48+
}

0 commit comments

Comments
 (0)