Skip to content

Commit 944fb99

Browse files
joshlongmarkpollack
authored andcommitted
Add extension function for Chatclient.CalLResponseSpec.entity(Class<?>)
- This makes ChatClient easier to use from Kotlin
1 parent fb0d99d commit 944fb99

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2023-2024 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.ai.chat.client
18+
19+
import org.springframework.ai.chat.model.ChatResponse
20+
import org.springframework.core.ParameterizedTypeReference
21+
22+
/**
23+
* Extensions for [ChatClient] providing a reified generic adapters for `entity` and `responseEntity`
24+
*
25+
* @author Josh Long
26+
*/
27+
28+
inline fun <reified T> ChatClient.CallResponseSpec.entity(): T =
29+
entity(object : ParameterizedTypeReference<T>() {}) as T
30+
31+
inline fun <reified T> ChatClient.CallResponseSpec.responseEntity(): ResponseEntity<ChatResponse, T> =
32+
responseEntity(object : ParameterizedTypeReference<T>() {})
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2023-2024 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.ai.chat.client
18+
19+
import io.mockk.every
20+
import io.mockk.mockk
21+
import io.mockk.verify
22+
import org.junit.jupiter.api.Test
23+
import org.springframework.ai.chat.model.ChatResponse
24+
import org.springframework.core.ParameterizedTypeReference
25+
26+
class ChatClientExtensionsTests {
27+
28+
data class Joke(val setup: String, val punchline: String)
29+
30+
@Test
31+
fun responseEntity() {
32+
val crs = mockk<ChatClient.CallResponseSpec>()
33+
val re = mockk<ResponseEntity<ChatResponse, Joke>>()
34+
every { crs.responseEntity<Joke>() } returns re
35+
crs.responseEntity<Joke>()
36+
verify { crs.responseEntity(object : ParameterizedTypeReference<Joke>() {}) }
37+
}
38+
39+
@Test
40+
fun entity() {
41+
val crs = mockk<ChatClient.CallResponseSpec>()
42+
val joke = mockk<Joke>()
43+
every { crs.entity(any<ParameterizedTypeReference<Joke>>()) } returns joke
44+
crs.entity<Joke>()
45+
verify { crs.entity(object : ParameterizedTypeReference<Joke>(){}) }
46+
}
47+
}

0 commit comments

Comments
 (0)