From 3eb3888b685492525f0f04dc85ce860a963f514b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 16 Jul 2026 15:31:51 +0000 Subject: [PATCH 1/3] Initial plan From e4004895f22123c81e6eb5ec9180d96b32eaf6f2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 16 Jul 2026 15:59:47 +0000 Subject: [PATCH 2/3] Add contentEncoding: base64 to JSON schema for byte[], Memory, ReadOnlyMemory Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com> --- .../src/System/Text/Json/Schema/JsonSchema.cs | 10 ++++++++++ .../Converters/Value/ByteArrayConverter.cs | 2 +- .../Converters/Value/MemoryByteConverter.cs | 2 +- .../Converters/Value/ReadOnlyMemoryByteConverter.cs | 2 +- .../tests/Common/JsonSchemaExporterTests.TestTypes.cs | 6 +++--- 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs index 8ffc12bd077926..a08487d5c3dd45 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs @@ -23,6 +23,7 @@ internal sealed class JsonSchema internal const string AnyOfPropertyName = "anyOf"; internal const string ConstPropertyName = "const"; internal const string DefaultPropertyName = "default"; + internal const string ContentEncodingPropertyName = "contentEncoding"; internal const string MinLengthPropertyName = "minLength"; internal const string MaxLengthPropertyName = "maxLength"; @@ -95,6 +96,9 @@ public JsonSchema() { } public int? MaxLength { get => _maxLength; set { VerifyMutable(); _maxLength = value; } } private int? _maxLength; + public string? ContentEncoding { get => _contentEncoding; set { VerifyMutable(); _contentEncoding = value; } } + private string? _contentEncoding; + public JsonSchemaExporterContext? ExporterContext { get; set; } public int KeywordCount @@ -124,6 +128,7 @@ public int KeywordCount Count(HasDefaultValue); Count(MinLength != null); Count(MaxLength != null); + Count(ContentEncoding != null); return count; @@ -255,6 +260,11 @@ public JsonNode ToJsonNode(JsonSchemaExporterOptions options) objSchema.Add(MaxLengthPropertyName, (JsonNode)maxLength); } + if (ContentEncoding != null) + { + objSchema.Add(ContentEncodingPropertyName, ContentEncoding); + } + return CompleteSchema(objSchema); JsonNode CompleteSchema(JsonNode schema) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/ByteArrayConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/ByteArrayConverter.cs index 93c2791d1525b0..dee25f37397614 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/ByteArrayConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/ByteArrayConverter.cs @@ -29,7 +29,7 @@ public override void Write(Utf8JsonWriter writer, byte[]? value, JsonSerializerO } } - internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String }; + internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String, ContentEncoding = "base64" }; internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.String; } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/MemoryByteConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/MemoryByteConverter.cs index eedb6f129708d5..f400e147e9b5af 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/MemoryByteConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/MemoryByteConverter.cs @@ -20,7 +20,7 @@ public override void Write(Utf8JsonWriter writer, Memory value, JsonSerial writer.WriteBase64StringValue(value.Span); } - internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String }; + internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String, ContentEncoding = "base64" }; internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.String; } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/ReadOnlyMemoryByteConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/ReadOnlyMemoryByteConverter.cs index 59aade6c5c24c1..412b865703f77c 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/ReadOnlyMemoryByteConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/ReadOnlyMemoryByteConverter.cs @@ -20,7 +20,7 @@ public override void Write(Utf8JsonWriter writer, ReadOnlyMemory value, Js writer.WriteBase64StringValue(value.Span); } - internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String }; + internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String, ContentEncoding = "base64" }; internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.String; } diff --git a/src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.TestTypes.cs b/src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.TestTypes.cs index 065f18e501d5ec..68c993436e82a8 100644 --- a/src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.TestTypes.cs +++ b/src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.TestTypes.cs @@ -57,10 +57,10 @@ public static IEnumerable GetTestDataCore() yield return new TestData( Value: [1, 2, 3], AdditionalValues: [[]], - ExpectedJsonSchema: """{"type":["string","null"]}"""); + ExpectedJsonSchema: """{"type":["string","null"],"contentEncoding":"base64"}"""); - yield return new TestData>(new byte[] { 1, 2, 3 }, ExpectedJsonSchema: """{"type":"string"}"""); - yield return new TestData>(new byte[] { 1, 2, 3 }, ExpectedJsonSchema: """{"type":"string"}"""); + yield return new TestData>(new byte[] { 1, 2, 3 }, ExpectedJsonSchema: """{"type":"string","contentEncoding":"base64"}"""); + yield return new TestData>(new byte[] { 1, 2, 3 }, ExpectedJsonSchema: """{"type":"string","contentEncoding":"base64"}"""); yield return new TestData( Value: new(2024, 06, 06, 21, 39, 42, DateTimeKind.Utc), ExpectedJsonSchema: """{"type":"string","format":"date-time"}"""); From 99b1a955bde696f8628f671f6d365c4fe778b5b3 Mon Sep 17 00:00:00 2001 From: Youssef Fahmy Date: Wed, 22 Jul 2026 16:42:26 +0200 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../src/System/Text/Json/Schema/JsonSchema.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs index 0242ad7d98c796..bde68b7f31c769 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs @@ -82,8 +82,7 @@ public JsonSchema() { } public bool? Deprecated { get; set { VerifyMutable(); field = value; } } - public string? ContentEncoding { get => _contentEncoding; set { VerifyMutable(); _contentEncoding = value; } } - private string? _contentEncoding; + public string? ContentEncoding { get; set { VerifyMutable(); field = value; } } public JsonSchemaExporterContext? ExporterContext { get; set; } @@ -115,7 +114,7 @@ public int KeywordCount Count(MinLength is not null); Count(MaxLength is not null); Count(Deprecated is not null); - Count(ContentEncoding != null); + Count(ContentEncoding is not null); return count; @@ -252,7 +251,7 @@ public JsonNode ToJsonNode(JsonSchemaExporterOptions options) objSchema.Add(DeprecatedPropertyName, (JsonNode)deprecated); } - if (ContentEncoding != null) + if (ContentEncoding is not null) { objSchema.Add(ContentEncodingPropertyName, ContentEncoding); }