Omit null mimeType from ResourceContents JSON serialization (#1325)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> Co-authored-by: Stephen Toub <stoub@microsoft.com>
This commit is contained in:
parent
54283a8590
commit
3727dc4aaa
2 changed files with 34 additions and 1 deletions
|
|
@ -158,7 +158,10 @@ public abstract class ResourceContents
|
|||
|
||||
writer.WriteStartObject();
|
||||
writer.WriteString("uri", value.Uri);
|
||||
writer.WriteString("mimeType", value.MimeType);
|
||||
if (value.MimeType is not null)
|
||||
{
|
||||
writer.WriteString("mimeType", value.MimeType);
|
||||
}
|
||||
|
||||
Debug.Assert(value is BlobResourceContents or TextResourceContents);
|
||||
if (value is BlobResourceContents blobResource)
|
||||
|
|
|
|||
|
|
@ -459,4 +459,34 @@ public static class ResourceContentsTests
|
|||
Assert.Equal(string.Empty, blobResource.Uri);
|
||||
Assert.Equal("YmxvYg==", System.Text.Encoding.UTF8.GetString(blobResource.Blob.ToArray()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void TextResourceContents_NullMimeType_OmittedFromJson()
|
||||
{
|
||||
var resource = new TextResourceContents
|
||||
{
|
||||
Uri = "file:///test.txt",
|
||||
MimeType = null,
|
||||
Text = "hello"
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize<ResourceContents>(resource, McpJsonUtilities.DefaultOptions);
|
||||
|
||||
Assert.DoesNotContain("mimeType", json);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void BlobResourceContents_NullMimeType_OmittedFromJson()
|
||||
{
|
||||
var resource = new BlobResourceContents
|
||||
{
|
||||
Uri = "file:///test.bin",
|
||||
MimeType = null,
|
||||
Blob = new byte[] { 1, 2, 3 }
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize<ResourceContents>(resource, McpJsonUtilities.DefaultOptions);
|
||||
|
||||
Assert.DoesNotContain("mimeType", json);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue