Add contentEncoding: base64 to JSON schema for byte[], Memory<byte>, and ReadOnlyMemory<byte> - #130881
#130881Add contentEncoding: base64 to JSON schema for byte[], Memory<byte>, and ReadOnlyMemory<byte>#130881Youssef1313 merged 5 commits intomaindotnet/runtime:mainfrom copilot/fix-jsonschemaexporter-byte-array-encodingdotnet/runtime:copilot/fix-jsonschemaexporter-byte-array-encodingCopy head branch name to clipboard
contentEncoding: base64 to JSON schema for byte[], Memory<byte>, and ReadOnlyMemory<byte>#130881Conversation
|
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. |
|
Tagging subscribers to this area: @dotnet/runtime-infrastructure |
…ReadOnlyMemory<byte> Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
contentEncoding: base64 to JSON schema for byte[], Memory<byte>, and ReadOnlyMemory<byte>
|
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. |
There was a problem hiding this comment.
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
ContentEncodingsupport to the internalJsonSchemamodel (KeywordCount+ JSON emission). - Updated the byte-oriented converters’
GetSchema()implementations to setContentEncoding = "base64". - Updated
JsonSchemaExporterTestsexpectations 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. |
|
Failure is a fix was merged 5 hours ago in #130892 |
|
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
}
]
} |
There was a problem hiding this comment.
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
|
@eiriktsarpalis Could we consider this for .NET 11 Preview 7 (snapped already yesterday) please? |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sure, if you could take care of the backporting process. |
|
/backport to release/11.0-preview7 |
|
Started backporting to |
|
@Youssef1313 backporting to |
|
/backport to release/11.0-preview7 |
|
Started backporting to |
|
@Youssef1313 backporting to 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 |
…yte[], Memory, and ReadOnlyMemory Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
JsonSchemaExporterwas not emitting"contentEncoding": "base64"for byte-array types, despite STJ serializing them as base64 strings. Per JSON Schema 2020-12 §8.3,contentEncodingshould be set when the string payload is encoded.Schema changes:
ContentEncodingproperty andContentEncodingPropertyName = "contentEncoding"constant to the internalJsonSchemaclass, following the same pattern asFormat,Pattern, etc.KeywordCountandToJsonNodeto includeContentEncodingConverter changes — updated
GetSchema()to returnContentEncoding = "base64"in:ByteArrayConverterMemoryByteConverterReadOnlyMemoryByteConverterBefore / After: