File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed
commonMain/kotlin/com.aallam.openai.api/chat
commonTest/kotlin/com.aallam.openai.api/chat Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import kotlinx.serialization.json.JsonDecoder
15
15
import kotlinx.serialization.json.JsonEncoder
16
16
import kotlinx.serialization.json.JsonObject
17
17
import kotlinx.serialization.json.JsonPrimitive
18
+ import kotlinx.serialization.json.jsonPrimitive
18
19
import kotlin.jvm.JvmInline
19
20
20
21
/* *
@@ -62,7 +63,7 @@ internal object FunctionModeSerializer : KSerializer<FunctionMode> {
62
63
require(decoder is JsonDecoder ) { " This decoder is not a JsonDecoder. Cannot deserialize `FunctionCall`" }
63
64
return when (val json = decoder.decodeJsonElement()) {
64
65
is JsonPrimitive -> Default (json.content)
65
- is JsonObject -> Named .serializer().deserialize(decoder )
66
+ is JsonObject -> json[ " name " ]?.jsonPrimitive?.content?. let ( FunctionMode :: Named ) ? : error( " Missing 'name' " )
66
67
else -> throw UnsupportedOperationException (" Cannot deserialize FunctionMode. Unsupported JSON element." )
67
68
}
68
69
}
Original file line number Diff line number Diff line change
1
+ package com.aallam.openai.api.chat
2
+
3
+ import com.aallam.openai.api.BetaOpenAI
4
+ import kotlinx.serialization.json.Json
5
+ import kotlin.test.Test
6
+ import kotlin.test.assertEquals
7
+
8
+ @OptIn(BetaOpenAI ::class )
9
+ class TestFunctionMode {
10
+
11
+ @Test
12
+ fun serialize () {
13
+ listOf (
14
+ FunctionMode .Auto ,
15
+ FunctionMode .None ,
16
+ FunctionMode .Named (" someFunctionName" )
17
+ ).forEach { functionMode ->
18
+ val jsonString = Json .encodeToString(FunctionMode .serializer(), functionMode)
19
+ val decoded = Json .decodeFromString(FunctionMode .serializer(), jsonString)
20
+ assertEquals(functionMode, decoded)
21
+ }
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments