Skip to content

Commit fb568bb

Browse files
authored
fix(client) add encodeDefault for field with non spec default value (#99)
* fix(client) add encodeDefault for field with non spec default value * add test
1 parent 9341f1e commit fb568bb

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/types.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,8 @@ public data class Tool(
10541054
val properties: JsonObject = EmptyJsonObject,
10551055
val required: List<String>? = null,
10561056
) {
1057+
@OptIn(ExperimentalSerializationApi::class)
1058+
@EncodeDefault
10571059
val type: String = "object"
10581060
}
10591061
}

src/commonTest/kotlin/ToolSerializationTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.modelcontextprotocol.kotlin.sdk
33
import io.kotest.assertions.json.shouldEqualJson
44
import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
55
import kotlinx.serialization.encodeToString
6+
import kotlinx.serialization.json.Json
67
import kotlinx.serialization.json.JsonPrimitive
78
import kotlinx.serialization.json.buildJsonObject
89
import kotlin.test.Test
@@ -54,4 +55,11 @@ class ToolSerializationTest {
5455
assertEquals(expected = getWeatherTool, actual = tool)
5556
}
5657

58+
@Test
59+
fun `should always serialize default value`() {
60+
val json = Json(from = McpJson) {
61+
encodeDefaults = false
62+
}
63+
json.encodeToString(getWeatherTool) shouldEqualJson getWeatherToolJson
64+
}
5765
}

0 commit comments

Comments
 (0)