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
This commit is contained in:
shiqicao 2025-05-08 23:52:40 -07:00 • committed by GitHub
commit fb568bbcab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 0 deletions

View file

@ -1054,6 +1054,8 @@ public data class Tool(
val properties: JsonObject = EmptyJsonObject,
val required: List<String>? = null,
) {
@OptIn(ExperimentalSerializationApi::class)
@EncodeDefault
val type: String = "object"
}
}

View file

@ -3,6 +3,7 @@ package io.modelcontextprotocol.kotlin.sdk
import io.kotest.assertions.json.shouldEqualJson
import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.buildJsonObject
import kotlin.test.Test
@ -54,4 +55,11 @@ class ToolSerializationTest {
assertEquals(expected = getWeatherTool, actual = tool)
}
@Test
fun `should always serialize default value`() {
val json = Json(from = McpJson) {
encodeDefaults = false
}
json.encodeToString(getWeatherTool) shouldEqualJson getWeatherToolJson
}
}