Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add contentEncoding: base64 to JSON schema for byte[], Memory<byte>, and ReadOnlyMemory<byte> - #130881

#130881
Merged
Youssef1313 merged 5 commits into
maindotnet/runtime:mainfrom
copilot/fix-jsonschemaexporter-byte-array-encodingdotnet/runtime:copilot/fix-jsonschemaexporter-byte-array-encodingCopy head branch name to clipboard
Jul 27, 2026
Merged

Add contentEncoding: base64 to JSON schema for byte[], Memory<byte>, and ReadOnlyMemory<byte>#130881
Youssef1313 merged 5 commits into
maindotnet/runtime:mainfrom
copilot/fix-jsonschemaexporter-byte-array-encodingdotnet/runtime:copilot/fix-jsonschemaexporter-byte-array-encodingCopy head branch name to clipboard

Conversation

Copilot AI commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

JsonSchemaExporter was not emitting "contentEncoding": "base64" for byte-array types, despite STJ serializing them as base64 strings. Per JSON Schema 2020-12 §8.3, contentEncoding should be set when the string payload is encoded.

Schema changes:

  • Added ContentEncoding property and ContentEncodingPropertyName = "contentEncoding" constant to the internal JsonSchema class, following the same pattern as Format, Pattern, etc.
  • Updated KeywordCount and ToJsonNode to include ContentEncoding

Converter changes — updated GetSchema() to return ContentEncoding = "base64" in:

  • ByteArrayConverter
  • MemoryByteConverter
  • ReadOnlyMemoryByteConverter

Before / After:

// Before
{ "type": ["string", "null"] }

// After — byte[]
{ "type": ["string", "null"], "contentEncoding": "base64" }

// After — Memory<byte> / ReadOnlyMemory<byte>
{ "type": "string", "contentEncoding": "base64" }

Copilot AI requested review from Copilot and removed request for Copilot July 16, 2026 15:31
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
15 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/runtime-infrastructure
See info in area-owners.md if you want to be subscribed.

…ReadOnlyMemory<byte>

Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot July 16, 2026 15:59
Copilot AI changed the title [WIP] Fix JsonSchemaExporter interaction with byte[] for contentEncoding Add contentEncoding: base64 to JSON schema for byte[], Memory<byte>, and ReadOnlyMemory<byte> Jul 16, 2026
Copilot AI requested a review from Youssef1313 July 16, 2026 16:00

@eiriktsarpalis eiriktsarpalis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@Youssef1313
Youssef1313 marked this pull request as ready for review July 16, 2026 18:17
Copilot AI review requested due to automatic review settings July 16, 2026 18:17
Copilot AI temporarily deployed to copilot-pat-pool July 16, 2026 18:17 Inactive
Copilot AI temporarily deployed to copilot-pat-pool July 16, 2026 18:18 Inactive
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates System.Text.Json’s JSON Schema export so that schemas for byte[], Memory<byte>, and ReadOnlyMemory<byte> explicitly describe their base64 string encoding via the JSON Schema contentEncoding keyword.

Changes:

  • Added ContentEncoding support to the internal JsonSchema model (KeywordCount + JSON emission).
  • Updated the byte-oriented converters’ GetSchema() implementations to set ContentEncoding = "base64".
  • Updated JsonSchemaExporterTests expectations for the affected types.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.TestTypes.cs Updates expected schemas for byte[], Memory<byte>, and ReadOnlyMemory<byte> to include contentEncoding.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/ReadOnlyMemoryByteConverter.cs Emits contentEncoding: "base64" for ReadOnlyMemory<byte> schemas.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/MemoryByteConverter.cs Emits contentEncoding: "base64" for Memory<byte> schemas.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/ByteArrayConverter.cs Emits contentEncoding: "base64" for byte[] schemas.
src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs Adds ContentEncoding keyword plumbing and serialization to JSON nodes.

@Youssef1313

Copy link
Copy Markdown
Member

Failure is

D:\a\_work\1\s\src\libraries\Microsoft.Extensions.Caching.Abstractions\tests\TrimmingTests\HybridCacheEntryOptionsAccessorTest.cs(17,6): error CS0246: The type or namespace name 'UnsafeAccessorAttribute' could not be found (are you missing a using directive or an assembly reference?) [D:\a\_work\1\s\src\libraries\Microsoft.Extensions.Caching.Abstractions\tests\Microsoft.Extensions.Caching.Abstractions.Tests.csproj::TargetFramework=net481]
D:\a\_work\1\s\src\libraries\Microsoft.Extensions.Caching.Abstractions\tests\TrimmingTests\HybridCacheEntryOptionsAccessorTest.cs(17,6): error CS0246: The type or namespace name 'UnsafeAccessor' could not be found (are you missing a using directive or an assembly reference?) [D:\a\_work\1\s\src\libraries\Microsoft.Extensions.Caching.Abstractions\tests\Microsoft.Extensions.Caching.Abstractions.Tests.csproj::TargetFramework=net481]
D:\a\_work\1\s\src\libraries\Microsoft.Extensions.Caching.Abstractions\tests\TrimmingTests\HybridCacheEntryOptionsAccessorTest.cs(17,21): error CS0103: The name 'UnsafeAccessorKind' does not exist in the current context [D:\a\_work\1\s\src\libraries\Microsoft.Extensions.Caching.Abstractions\tests\Microsoft.Extensions.Caching.Abstractions.Tests.csproj::TargetFramework=net481]

a fix was merged 5 hours ago in #130892

Copilot AI review requested due to automatic review settings July 17, 2026 14:45
@Youssef1313
Youssef1313 temporarily deployed to copilot-pat-pool July 17, 2026 14:45 — with GitHub Actions Inactive
@github-actions

github-actions Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Workflow state for the Holistic Review Orchestrator.

{
  "version": 5,
  "last_dispatched_commit": "ebe741af69515dca08f4d05e97e73fc74cd6c2e9",
  "last_dispatched_base_ref": "main",
  "last_dispatched_base_sha": "455968886e6286974cc150d9ab1634c8e7ddf9a0",
  "last_reviewed_commit": "ebe741af69515dca08f4d05e97e73fc74cd6c2e9",
  "last_reviewed_base_ref": "main",
  "last_reviewed_base_sha": "455968886e6286974cc150d9ab1634c8e7ddf9a0",
  "last_recorded_worker_run_id": "29686420391",
  "review_attempt_commit": "",
  "review_attempt_base_ref": "",
  "review_attempt_count": 0,
  "max_review_attempts": 5,
  "review_history_format": "holistic-review-disclosure-v1",
  "review_history": [
    {
      "commit": "ebe741af69515dca08f4d05e97e73fc74cd6c2e9",
      "review_id": 4730732629
    }
  ]
}

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Holistic Review

Motivation: JsonSchemaExporter serializes byte[], Memory<byte>, and ReadOnlyMemory<byte> as base64-encoded JSON strings, but the emitted schema only declared "type": "string" without indicating the encoding. Per JSON Schema 2020-12 §8.3, contentEncoding should describe how the string payload is encoded, so consumers of the generated schema can validate and interpret the value correctly. The motivation is sound and verifiable from the converters, which all call WriteBase64StringValue.

Approach: The change adds a ContentEncoding string property (with a contentEncoding keyword constant) to the internal JsonSchema class, following the exact pattern already used for Format, Pattern, MinLength, and MaxLength. KeywordCount and ToJsonNode are both updated to include the new keyword, and the three byte-oriented value converters set ContentEncoding = "base64" in their GetSchema overrides. This is the minimal, idiomatic way to extend the internal schema model and is consistent with the surrounding code.

Summary: The implementation is correct and complete. KeywordCount is kept in sync with ToJsonNode (important, since the exporter uses the count to decide between boolean and object schemas), the new property uses the same VerifyMutable mutability guard as its siblings, and the test expectations in JsonSchemaExporterTests.TestTypes.cs are updated to match the new output for all three types. The only externally observable effect is the addition of "contentEncoding": "base64" to these schemas, which is the intended improvement and is well covered by the existing round-trip schema tests. contentMediaType is intentionally omitted, which is reasonable since base64-encoded bytes have no inherent media type. No correctness, performance, or API-surface concerns (the changed JsonSchema type is internal). LGTM.

Note

This review was generated by this repository's Holistic Review agentic workflow to complement the built-in Copilot review.

Generated by Holistic Review · 37.5 AIC · ⌖ 9.49 AIC · ⊞ 10K

Copilot AI review requested due to automatic review settings July 22, 2026 14:35
@Youssef1313

Copy link
Copy Markdown
Member

@eiriktsarpalis Could we consider this for .NET 11 Preview 7 (snapped already yesterday) please?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Comment thread src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs Outdated
Comment thread src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs Outdated
Comment thread src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 22, 2026 14:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

@eiriktsarpalis

Copy link
Copy Markdown
Member

Could we consider this for .NET 11 Preview 7 (snapped already yesterday) please?

Sure, if you could take care of the backporting process.

@Youssef1313

Copy link
Copy Markdown
Member

/backport to release/11.0-preview7

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0-preview7 (link to workflow run)

@github-actions

Copy link
Copy Markdown
Contributor

@Youssef1313 backporting to release/11.0-preview7 was not run because the source pull request has not been merged. Please merge this pull request before requesting a backport.

@Youssef1313
Youssef1313 merged commit f561b0b into main Jul 27, 2026
82 of 85 checks passed
@Youssef1313
Youssef1313 deleted the copilot/fix-jsonschemaexporter-byte-array-encoding branch July 27, 2026 09:44
@Youssef1313

Copy link
Copy Markdown
Member

/backport to release/11.0-preview7

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0-preview7 (link to workflow run)

@github-actions

Copy link
Copy Markdown
Contributor

@Youssef1313 backporting to release/11.0-preview7 failed, the patch most likely resulted in conflicts. Please backport manually!

git am output
$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Creating an empty commit: Initial plan
Applying: Add contentEncoding: base64 to JSON schema for byte[], Memory<byte>, ReadOnlyMemory<byte>
Using index info to reconstruct a base tree...
M	src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs
M	src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/ByteArrayConverter.cs
M	src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.TestTypes.cs
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs
CONFLICT (content): Merge conflict in src/libraries/System.Text.Json/src/System/Text/Json/Schema/JsonSchema.cs
Auto-merging src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/ByteArrayConverter.cs
Auto-merging src/libraries/System.Text.Json/tests/Common/JsonSchemaExporterTests.TestTypes.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0002 Add contentEncoding: base64 to JSON schema for byte[], Memory<byte>, ReadOnlyMemory<byte>
Error: The process '/usr/bin/git' failed with exit code 128

Link to workflow output

Copilot AI added a commit that referenced this pull request Jul 27, 2026
…yte[], Memory, and ReadOnlyMemory

Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

JsonSchemaExporter interaction with byte[]

4 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.