-
Notifications
You must be signed in to change notification settings - Fork 681
Expand file tree
/
Copy pathMcpJsonUtilitiesTests.cs
More file actions
86 lines (71 loc) · 3.15 KB
/
McpJsonUtilitiesTests.cs
File metadata and controls
86 lines (71 loc) · 3.15 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
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using ModelContextProtocol.Protocol;
namespace ModelContextProtocol.Tests;
public static class McpJsonUtilitiesTests
{
[Fact]
public static void DefaultOptions_IsSingleton()
{
var options = McpJsonUtilities.DefaultOptions;
Assert.NotNull(options);
Assert.True(options.IsReadOnly);
Assert.Same(options, McpJsonUtilities.DefaultOptions);
}
[Fact]
public static void DefaultOptions_SupportsAnonymousTypes()
{
// DefaultOptions includes a fallback DefaultJsonTypeInfoResolver to support
// serialization of user-defined types including anonymous types, regardless
// of the IsReflectionEnabledByDefault setting.
var options = McpJsonUtilities.DefaultOptions;
Type anonType = new { Id = 42 }.GetType();
Assert.True(options.TryGetTypeInfo(anonType, out _));
}
[Fact]
public static void DefaultOptions_UnknownEnumHandling()
{
var options = McpJsonUtilities.DefaultOptions;
if (JsonSerializer.IsReflectionEnabledByDefault)
{
Assert.Equal("\"A\"", JsonSerializer.Serialize(EnumWithoutAnnotation.A, options));
Assert.Equal("\"A\"", JsonSerializer.Serialize(EnumWithAnnotation.A, options));
}
else
{
options = new(options) { TypeInfoResolver = new DefaultJsonTypeInfoResolver() };
Assert.Equal("1", JsonSerializer.Serialize(EnumWithoutAnnotation.A, options));
Assert.Equal("\"A\"", JsonSerializer.Serialize(EnumWithAnnotation.A, options));
}
}
[Fact]
public static void DefaultOptions_CanSerializeIEnumerableOfContentBlock()
{
var options = McpJsonUtilities.DefaultOptions;
// Create an IEnumerable<ContentBlock> with different content types
IEnumerable<ContentBlock> contentBlocks = new List<ContentBlock>
{
new TextContentBlock { Text = "Hello World" },
new TextContentBlock { Text = "Test message" }
};
// Should not throw NotSupportedException
string json = JsonSerializer.Serialize(contentBlocks, options);
Assert.NotNull(json);
Assert.Contains("Hello World", json);
Assert.Contains("Test message", json);
Assert.Contains("\"type\":\"text\"", json);
// Should also be able to deserialize back
var deserialized = JsonSerializer.Deserialize<IEnumerable<ContentBlock>>(json, options);
Assert.NotNull(deserialized);
var deserializedList = deserialized.ToList();
Assert.Equal(2, deserializedList.Count);
Assert.All(deserializedList, cb => Assert.Equal("text", cb.Type));
var textBlocks = deserializedList.Cast<TextContentBlock>().ToArray();
Assert.Equal("Hello World", textBlocks[0].Text);
Assert.Equal("Test message", textBlocks[1].Text);
}
public enum EnumWithoutAnnotation { A = 1, B = 2, C = 3 }
[JsonConverter(typeof(JsonStringEnumConverter<EnumWithAnnotation>))]
public enum EnumWithAnnotation { A = 1, B = 2, C = 3 }
}