Repository navigation
Expand file tree
/
Copy pathSchemaEvolutionTests.java
More file actions
231 lines (193 loc) · 8.61 KB
/
Copy pathSchemaEvolutionTests.java
File metadata and controls
231 lines (193 loc) · 8.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Copyright 2026 - 2026 the original author or authors.
*/
package io.modelcontextprotocol.spec;
import static io.modelcontextprotocol.util.McpJsonMapperUtils.JSON_MAPPER;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import io.modelcontextprotocol.json.McpJsonMapper;
import org.junit.jupiter.api.Test;
/**
* Forward/backward compatibility tests for wire-serialized records:
* <ul>
* <li>Unknown fields are ignored (forward compat: old client, new server).</li>
* <li>Optional fields absent from wire deserialize to {@code null} (backward
* compat).</li>
* <li>Null optional fields are omitted from serialized output ({@code NON_ABSENT}).</li>
* </ul>
*/
class SchemaEvolutionTests {
private final McpJsonMapper mapper = JSON_MAPPER;
// -----------------------------------------------------------------------
// TextContent
// -----------------------------------------------------------------------
@Test
void textContentUnknownFieldsIgnored() throws IOException {
String json = """
{"type":"text","text":"hi","newFieldFromFutureVersion":"ignored","nested":{"a":1}}
""";
McpSchema.TextContent content = mapper.readValue(json, McpSchema.TextContent.class);
assertThat(content.text()).isEqualTo("hi");
}
@Test
void textContentNullAnnotationsOmitted() throws IOException {
McpSchema.TextContent content = McpSchema.TextContent.builder("hello").build();
String json = mapper.writeValueAsString(content);
assertThat(json).doesNotContain("annotations");
}
// -----------------------------------------------------------------------
// Prompt — null arguments must NOT coerce to empty list on the wire
// -----------------------------------------------------------------------
@Test
void promptWithNullArgumentsDeserializesAsNull() throws IOException {
String json = """
{"name":"p","description":"desc"}
""";
McpSchema.Prompt prompt = mapper.readValue(json, McpSchema.Prompt.class);
assertThat(prompt.arguments()).isNull();
}
@Test
void promptWithNullArgumentsOmitsFieldOnWire() throws IOException {
McpSchema.Prompt prompt = McpSchema.Prompt.builder("p").description("desc").build();
String json = mapper.writeValueAsString(prompt);
assertThat(json).doesNotContain("arguments");
}
@Test
void promptUnknownFieldsIgnored() throws IOException {
String json = """
{"name":"p","description":"desc","futureField":true}
""";
McpSchema.Prompt prompt = mapper.readValue(json, McpSchema.Prompt.class);
assertThat(prompt.name()).isEqualTo("p");
}
// -----------------------------------------------------------------------
// InitializeRequest
// -----------------------------------------------------------------------
@Test
void initializeRequestUnknownFieldsIgnored() throws IOException {
String json = """
{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"test","version":"1"},
"unknownFuture":"value"}
""";
McpSchema.InitializeRequest req = mapper.readValue(json, McpSchema.InitializeRequest.class);
assertThat(req.protocolVersion()).isEqualTo("2025-06-18");
}
// -----------------------------------------------------------------------
// CompleteCompletion — NON_ABSENT (was ALWAYS)
// -----------------------------------------------------------------------
@Test
void completeCompletionOmitsNullOptionals() throws IOException {
McpSchema.CompleteResult.CompleteCompletion c = new McpSchema.CompleteResult.CompleteCompletion(List.of("x"));
String json = mapper.writeValueAsString(c);
assertThat(json).doesNotContain("total");
assertThat(json).doesNotContain("hasMore");
}
@Test
void completeCompletionUnknownFieldsIgnored() throws IOException {
String json = """
{"values":["a","b"],"newField":99}
""";
McpSchema.CompleteResult.CompleteCompletion c = mapper.readValue(json,
McpSchema.CompleteResult.CompleteCompletion.class);
assertThat(c.values()).containsExactly("a", "b");
}
// -----------------------------------------------------------------------
// LoggingLevel — lenient deserialization via @JsonCreator
// -----------------------------------------------------------------------
@Test
void loggingLevelDeserializesFromString() throws IOException {
String json = "\"warning\"";
McpSchema.LoggingLevel level = mapper.readValue(json, McpSchema.LoggingLevel.class);
assertThat(level).isEqualTo(McpSchema.LoggingLevel.WARNING);
}
@Test
void loggingLevelUnknownValueReturnsNull() throws IOException {
String json = "\"nonexistent\"";
McpSchema.LoggingLevel level = mapper.readValue(json, McpSchema.LoggingLevel.class);
assertThat(level).isNull();
}
// -----------------------------------------------------------------------
// ServerCapabilities nested records — unknown fields
// -----------------------------------------------------------------------
@Test
void serverCapabilitiesUnknownFieldsIgnored() throws IOException {
String json = """
{"tools":{"listChanged":true,"futureField":"x"},"unknownCap":{}}
""";
McpSchema.ServerCapabilities caps = mapper.readValue(json, McpSchema.ServerCapabilities.class);
assertThat(caps.tools()).isNotNull();
assertThat(caps.tools().listChanged()).isTrue();
}
@Test
void serverCapabilitiesWithoutExtensionsDeserializesAsNull() throws IOException {
String json = """
{"tools":{"listChanged":true}}
""";
McpSchema.ServerCapabilities caps = mapper.readValue(json, McpSchema.ServerCapabilities.class);
assertThat(caps.extensions()).isNull();
}
@Test
void serverCapabilitiesWithNullExtensionsOmitsFieldOnWire() throws IOException {
String json = mapper.writeValueAsString(McpSchema.ServerCapabilities.builder().tools(true).build());
assertThat(json).doesNotContain("extensions");
}
@Test
void serverCapabilitiesExtensionsRoundTrip() throws IOException {
Map<String, Map<String, Object>> extensions = Map.of("com.example/ext-with-settings", Map.of("maxDepth", 3),
"com.example/ext-without-settings", Map.of());
McpSchema.ServerCapabilities caps = McpSchema.ServerCapabilities.builder().extensions(extensions).build();
String json = mapper.writeValueAsString(caps);
assertThat(json).contains("\"com.example/ext-without-settings\":{}");
McpSchema.ServerCapabilities parsed = mapper.readValue(json, McpSchema.ServerCapabilities.class);
assertThat(parsed.extensions()).isEqualTo(extensions);
}
// -----------------------------------------------------------------------
// ClientCapabilities.extensions
// -----------------------------------------------------------------------
@Test
void clientCapabilitiesWithoutExtensionsDeserializesAsNull() throws IOException {
String json = """
{"roots":{"listChanged":true},"sampling":{}}
""";
McpSchema.ClientCapabilities caps = mapper.readValue(json, McpSchema.ClientCapabilities.class);
assertThat(caps.extensions()).isNull();
}
@Test
void clientCapabilitiesWithNullExtensionsOmitsFieldOnWire() throws IOException {
String json = mapper.writeValueAsString(McpSchema.ClientCapabilities.builder().sampling().build());
assertThat(json).doesNotContain("extensions");
}
@Test
void clientCapabilitiesWithExtensionsUnknownFieldsIgnored() throws IOException {
String json = """
{"extensions":{"io.modelcontextprotocol/tasks":{}},"futureCapability":{"a":1}}
""";
McpSchema.ClientCapabilities caps = mapper.readValue(json, McpSchema.ClientCapabilities.class);
assertThat(caps.extensions()).containsOnlyKeys("io.modelcontextprotocol/tasks");
}
@Test
void clientCapabilitiesExtensionsRoundTrip() throws IOException {
Map<String, Map<String, Object>> extensions = Map.of("com.example/ext-with-settings", Map.of("maxDepth", 3),
"com.example/ext-without-settings", Map.of());
McpSchema.ClientCapabilities caps = McpSchema.ClientCapabilities.builder().extensions(extensions).build();
String json = mapper.writeValueAsString(caps);
assertThat(json).contains("\"com.example/ext-without-settings\":{}");
McpSchema.ClientCapabilities parsed = mapper.readValue(json, McpSchema.ClientCapabilities.class);
assertThat(parsed.extensions()).isEqualTo(extensions);
}
// -----------------------------------------------------------------------
// JSONRPCError
// -----------------------------------------------------------------------
@Test
void jsonRpcErrorUnknownFieldsIgnored() throws IOException {
String json = """
{"code":-32601,"message":"Not found","futureData":{"detail":"x"}}
""";
McpSchema.JSONRPCResponse.JSONRPCError error = mapper.readValue(json,
McpSchema.JSONRPCResponse.JSONRPCError.class);
assertThat(error.code()).isEqualTo(-32601);
assertThat(error.message()).isEqualTo("Not found");
}
}