-
Notifications
You must be signed in to change notification settings - Fork 689
Expand file tree
/
Copy pathUnknownPropertiesTests.cs
More file actions
258 lines (227 loc) · 8.62 KB
/
UnknownPropertiesTests.cs
File metadata and controls
258 lines (227 loc) · 8.62 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System.Text.Json;
using ModelContextProtocol.Protocol;
namespace ModelContextProtocol.Tests.Protocol;
/// <summary>
/// Tests that custom JSON converters properly handle unknown additional properties
/// for forward compatibility with future protocol versions.
/// </summary>
public class UnknownPropertiesTests
{
[Fact]
public void ContentBlock_DeserializationWithUnknownProperty_SkipsProperty()
{
// Arrange - JSON with unknown "unknownField" property
const string Json = """
{
"type": "text",
"text": "Hello, world!",
"unknownField": "should be skipped"
}
""";
// Act
var deserialized = JsonSerializer.Deserialize<ContentBlock>(Json, McpJsonUtilities.DefaultOptions);
// Assert
Assert.NotNull(deserialized);
var textBlock = Assert.IsType<TextContentBlock>(deserialized);
Assert.Equal("Hello, world!", textBlock.Text);
}
[Fact]
public void ContentBlock_DeserializationWithStructuredContentInContent_SkipsProperty()
{
// Arrange - This was the actual bug case: structuredContent incorrectly placed
// inside a ContentBlock instead of at CallToolResult level
const string Json = """
{
"type": "text",
"text": "Result text",
"structuredContent": {
"data": "this should be ignored"
}
}
""";
// Act - Should not throw
var deserialized = JsonSerializer.Deserialize<ContentBlock>(Json, McpJsonUtilities.DefaultOptions);
// Assert
Assert.NotNull(deserialized);
var textBlock = Assert.IsType<TextContentBlock>(deserialized);
Assert.Equal("Result text", textBlock.Text);
}
[Fact]
public void ContentBlock_DeserializationWithMultipleUnknownProperties_SkipsAll()
{
// Arrange - JSON with multiple unknown properties
const string Json = """
{
"type": "image",
"data": "base64data",
"mimeType": "image/png",
"futureProperty1": "value1",
"futureProperty2": {"nested": "object"},
"futureProperty3": [1, 2, 3]
}
""";
// Act
var deserialized = JsonSerializer.Deserialize<ContentBlock>(Json, McpJsonUtilities.DefaultOptions);
// Assert
Assert.NotNull(deserialized);
var imageBlock = Assert.IsType<ImageContentBlock>(deserialized);
Assert.Equal("base64data", System.Text.Encoding.UTF8.GetString(imageBlock.Data.ToArray()));
Assert.Equal("image/png", imageBlock.MimeType);
}
[Fact]
public void Reference_DeserializationWithUnknownProperty_SkipsProperty()
{
// Arrange - JSON with unknown "metadata" property
const string Json = """
{
"type": "ref/prompt",
"name": "test-prompt",
"metadata": {
"version": "2.0",
"author": "future"
}
}
""";
// Act
var deserialized = JsonSerializer.Deserialize<Reference>(Json, McpJsonUtilities.DefaultOptions);
// Assert
Assert.NotNull(deserialized);
var promptRef = Assert.IsType<PromptReference>(deserialized);
Assert.Equal("test-prompt", promptRef.Name);
}
[Fact]
public void Reference_DeserializationWithMultipleUnknownProperties_SkipsAll()
{
// Arrange
const string Json = """
{
"type": "ref/resource",
"uri": "file:///test.txt",
"extraField1": "ignored",
"extraField2": 123,
"extraField3": true
}
""";
// Act
var deserialized = JsonSerializer.Deserialize<Reference>(Json, McpJsonUtilities.DefaultOptions);
// Assert
Assert.NotNull(deserialized);
var resourceRef = Assert.IsType<ResourceTemplateReference>(deserialized);
Assert.Equal("file:///test.txt", resourceRef.Uri);
}
[Fact]
public void ResourceContents_DeserializationWithUnknownProperty_SkipsProperty()
{
// Arrange
const string Json = """
{
"uri": "file:///document.txt",
"text": "File contents here",
"mimeType": "text/plain",
"futureEncoding": "utf-16",
"futureChecksum": "abc123"
}
""";
// Act
var deserialized = JsonSerializer.Deserialize<ResourceContents>(Json, McpJsonUtilities.DefaultOptions);
// Assert
Assert.NotNull(deserialized);
var textResource = Assert.IsType<TextResourceContents>(deserialized);
Assert.Equal("file:///document.txt", textResource.Uri);
Assert.Equal("File contents here", textResource.Text);
Assert.Equal("text/plain", textResource.MimeType);
}
[Fact]
public void ProgressNotificationParams_DeserializationWithUnknownProperty_SkipsProperty()
{
// Arrange
const string Json = """
{
"progressToken": "token123",
"progress": 50,
"newProgressFormat": "percentage",
"estimatedTimeRemaining": 30
}
""";
// Act
var deserialized = JsonSerializer.Deserialize<ProgressNotificationParams>(Json, McpJsonUtilities.DefaultOptions);
// Assert
Assert.NotNull(deserialized);
Assert.Equal("token123", deserialized.ProgressToken.ToString());
Assert.Equal(50.0f, deserialized.Progress.Progress);
}
[Fact]
public void PrimitiveSchemaDefinition_DeserializationWithUnknownProperty_SkipsProperty()
{
// Arrange
const string Json = """
{
"type": "string",
"description": "A test string",
"futureValidation": "regex:.*",
"futureMaxLength": 100
}
""";
// Act
var deserialized = JsonSerializer.Deserialize<ElicitRequestParams.PrimitiveSchemaDefinition>(Json, McpJsonUtilities.DefaultOptions);
// Assert
Assert.NotNull(deserialized);
var stringSchema = Assert.IsType<ElicitRequestParams.StringSchema>(deserialized);
Assert.Equal("A test string", stringSchema.Description);
}
[Fact]
public void CallToolResult_WithContentBlockContainingUnknownProperties_Succeeds()
{
// Arrange - Simulates the real-world bug scenario: a malformed response where
// structuredContent was incorrectly nested inside content blocks
const string Json = """
{
"content": [
{
"type": "text",
"text": "Tool executed successfully",
"structuredContent": {
"result": "this was incorrectly placed here"
}
}
],
"isError": false
}
""";
// Act - Should not throw an exception
var deserialized = JsonSerializer.Deserialize<CallToolResult>(Json, McpJsonUtilities.DefaultOptions);
// Assert
Assert.NotNull(deserialized);
Assert.Single(deserialized.Content);
var textBlock = Assert.IsType<TextContentBlock>(deserialized.Content[0]);
Assert.Equal("Tool executed successfully", textBlock.Text);
Assert.False(deserialized.IsError);
}
[Fact]
public void CallToolResult_WithStructuredContentAtCorrectLevel_PreservesProperty()
{
// Arrange - Correct placement of structuredContent at CallToolResult level
const string Json = """
{
"content": [
{
"type": "text",
"text": "Tool executed"
}
],
"structuredContent": {
"result": "correctly placed here",
"value": 42
},
"isError": false
}
""";
// Act
var deserialized = JsonSerializer.Deserialize<CallToolResult>(Json, McpJsonUtilities.DefaultOptions);
// Assert
Assert.NotNull(deserialized);
Assert.NotNull(deserialized.StructuredContent);
Assert.Equal("correctly placed here", deserialized.StructuredContent.Value.GetProperty("result").GetString());
Assert.Equal(42, deserialized.StructuredContent.Value.GetProperty("value").GetInt32());
}
}