Skip to content

Preserve original $schema version in ToJson() instead of forcing draft-04#1881

Merged
lahma merged 2 commits intomasterfrom
copilot/fix-fromfileasync-json-schema
Mar 1, 2026
Merged

Preserve original $schema version in ToJson() instead of forcing draft-04#1881
lahma merged 2 commits intomasterfrom
copilot/fix-fromfileasync-json-schema

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 1, 2026

JsonSchema.ToJson() unconditionally overwrote SchemaVersion with "http://json-schema.org/draft-04/schema#" before serializing, discarding any version already present on the schema (e.g. 2020-12). This caused FromFileAsync() to appear to return a schema with the wrong version whenever the result was serialized.

Changes

  • JsonSchema.cs: Changed ToJson(Formatting) to only apply the draft-04 default when SchemaVersion is null, leaving explicitly set versions intact.
  • JsonSchemaTests.cs: Added test verifying that a schema declaring 2020-12 retains that version through a FromJsonAsyncToJson round-trip.
// Before: always forced draft-04 regardless of what was in the file
SchemaVersion = "http://json-schema.org/draft-04/schema#";

// After: only applies draft-04 as default when no version is set
if (SchemaVersion == null)
{
    SchemaVersion = "http://json-schema.org/draft-04/schema#";
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • http://json-schema.org:80/draft-04/schema
    • Triggering command: /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net9.0/NJsonSchema.Tests /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net9.0/NJsonSchema.Tests @@ /tmp/tmpKLjJLR.tmp (http block)
    • Triggering command: /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests @@ /tmp/tmpiQ6LbH.tmp (http block)
    • Triggering command: /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests @@ /tmp/tmpVm5EzI.tmp (http block)
  • schemas.sportradar.com
    • Triggering command: /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net9.0/NJsonSchema.Tests /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net9.0/NJsonSchema.Tests @@ /tmp/tmpKLjJLR.tmp (dns block)
    • Triggering command: /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests @@ /tmp/tmpiQ6LbH.tmp (dns block)
    • Triggering command: /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests @@ /tmp/tmpVm5EzI.tmp (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>FromFileAsync forces wrong json schema version</issue_title>
<issue_description>The json schema contains
{
"$schema": "https://json-schema.org/draft/2020-12"
...
}

However, when read from file via JsonSchema.FromFileAsync(), the output is
{
"$schema": "http://json-schema.org/draft-04/schema#",
...
}

Mismatch in version is causing anyOf to stop working.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

… version set

Co-authored-by: lahma <171892+lahma@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix FromFileAsync to output correct json schema version Preserve original $schema version in ToJson() instead of forcing draft-04 Mar 1, 2026
Copilot finished work on behalf of lahma March 1, 2026 16:00
@lahma lahma marked this pull request as ready for review March 1, 2026 18:18
@lahma lahma merged commit fb3903b into master Mar 1, 2026
2 checks passed
This was referenced Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FromFileAsync forces wrong json schema version

2 participants