test: add serialization tests and guidelines for handling null and omitted fields (#583)

This commit is contained in:
Konstantin Pavlov 2026-03-04 18:59:07 +02:00 • committed by GitHub
commit 1454229ad9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 52 additions and 0 deletions

View file

@ -189,6 +189,34 @@ class InitializeTest {
)
}
@Test
fun `should serialize InitializeResult without meta - meta must be omitted`() {
val result = InitializeResult(
protocolVersion = "2025-03-26",
capabilities = ServerCapabilities(tools = ServerCapabilities.Tools(listChanged = false)),
serverInfo = Implementation(name = "test-server", version = "1.0.0"),
)
verifySerialization(
result,
McpJson,
"""
{
"protocolVersion": "2025-03-26",
"capabilities": {
"tools": {
"listChanged": false
}
},
"serverInfo": {
"name": "test-server",
"version": "1.0.0"
}
}
""".trimIndent(),
)
}
@Test
fun `should deserialize InitializeResult from JSON`() {
val json = """

View file

@ -265,6 +265,26 @@ class JsonRpcTest {
)
}
@Test
fun `should serialize JSONRPCResponse with EmptyResult - null meta must be omitted`() {
val response = JSONRPCResponse(
id = RequestId("call-2"),
result = EmptyResult(), // meta = null by default
)
verifySerialization(
response,
McpJson,
"""
{
"id": "call-2",
"result": {},
"jsonrpc": "2.0"
}
""".trimIndent(),
)
}
@Test
fun `should deserialize JSONRPCResponse with EmptyResult`() {
val json = """