diff --git a/spring-ai-core/src/main/kotlin/org/springframework/ai/chat/client/ChatClientExtensions.kt b/spring-ai-core/src/main/kotlin/org/springframework/ai/chat/client/ChatClientExtensions.kt new file mode 100644 index 000000000..ef05104a7 --- /dev/null +++ b/spring-ai-core/src/main/kotlin/org/springframework/ai/chat/client/ChatClientExtensions.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2023-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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.chat.client + +/** + * Extension for [ChatClient] providing a `entity()` variant. + * + * @author Josh Long + */ + +inline fun ChatClient.CallResponseSpec.entity(): T = entity(T::class.java) as T diff --git a/spring-ai-core/src/test/kotlin/org/springframework/ai/chat/client/ChatClientExtensionsTests.kt b/spring-ai-core/src/test/kotlin/org/springframework/ai/chat/client/ChatClientExtensionsTests.kt new file mode 100644 index 000000000..75fbacb2f --- /dev/null +++ b/spring-ai-core/src/test/kotlin/org/springframework/ai/chat/client/ChatClientExtensionsTests.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2023-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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.chat.client + +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import org.junit.jupiter.api.Test + +class ChatClientExtensionsTests { + + private val crs = mockk() + + data class Joke(val setup: String, val punchline: String) + + @Test + fun withEntityType() { + val joke = Joke( + setup = "Why did the scarecrow win an award?", + punchline = "Because he was outstanding in his field!" + ) + every { crs.entity(any>()) } returns joke + crs.entity() + verify { crs.entity(Joke::class.java) } + } +}