Skip to content

Fix ExtensionData merge behavior and add enum support for JsonSchemaExtensionDataAttribute#1888

Merged
lahma merged 2 commits intomasterfrom
copilot/add-jsonschema-sorting-feature
Mar 2, 2026
Merged

Fix ExtensionData merge behavior and add enum support for JsonSchemaExtensionDataAttribute#1888
lahma merged 2 commits intomasterfrom
copilot/add-jsonschema-sorting-feature

Conversation

Copy link
Contributor

Copilot AI commented Mar 1, 2026

ExtensionData is the correct mechanism for attaching custom metadata (e.g. tags for sorting) to JsonSchema objects, but two gaps limited its usefulness in practice.

Changes

  • Merge instead of replace in ApplyTypeExtensionDataAttributes: Previously, attribute-derived extension data replaced schema.ExtensionData entirely. This meant ISchemaProcessor implementations and [JsonSchemaExtensionData] attributes couldn't coexist — whichever ran second would lose the other's data. Now uses ??= + per-key assignment to merge into any existing dictionary.

  • Add AttributeTargets.Enum to JsonSchemaExtensionDataAttribute: The attribute was restricted to Class | Property | Parameter | ReturnValue, silently preventing use on enum types. Enum schemas go through the same ApplyTypeExtensionDataAttributes path, so adding Enum to the target list is sufficient.

Example: Combined attribute + processor usage (previously broken)

[JsonSchemaExtensionData("x-category", "important")]
public class MyModel { }

// ISchemaProcessor adds x-version without losing x-category
public class VersionSchemaProcessor : ISchemaProcessor
{
    public void Process(SchemaProcessorContext context)
    {
        context.Schema.ExtensionData ??= new Dictionary<string, object?>();
        context.Schema.ExtensionData["x-version"] = "v2";
    }
}
// Result: schema.ExtensionData = { "x-category": "important", "x-version": "v2" }

// Enum tagging (previously required AttributeTargets.Enum, now supported)
[JsonSchemaExtensionData("x-tag", "my-enum")]
public enum MyTaggedEnum { Value1, Value2 }

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_net8.0/NJsonSchema.Tests /home/REDACTED/work/NJsonSchema/NJsonSchema/artifacts/bin/NJsonSchema.Tests/debug_net8.0/NJsonSchema.Tests @@ /tmp/tmp4O57zD.tmp (http block)
    • 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/tmp4Z5kc1.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/tmpfJTclb.tmp /p:AllowMissingPrunePackageData=true (http block)
  • schemas.sportradar.com
    • 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/tmp4O57zD.tmp (dns block)
    • 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/tmp4Z5kc1.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/tmpfJTclb.tmp /p:AllowMissingPrunePackageData=true (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>Sorting JSonSchema(4) in NSwag (Documentation request, and maybe feature)</issue_title>
<issue_description>Is there any field in JSonSchema4/JSonSchema that can store additional information (like tags) in order to allow sorting models in NSwag using ISchemaProcessor to convert model's attributes to tags, and then a DocumentProcessor to sort models by tag first and then alphabetical order?

I'm asking there because since the Document in NSwag is made of NJsonSchema pieces, I need to know how those pieces works togheter, but I did not found any documentation on internals of JSonSchema so even if I could use some members maybe those were intended for some other usage and may break with future updates</issue_description>

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

@RicoSuter You can use the dictionary in ExtensionData which contains properties which are not predefined schema properties

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

Copilot AI changed the title [WIP] Add support for sorting models in NSwag using JSON Schema Fix ExtensionData merge behavior and add enum support for JsonSchemaExtensionDataAttribute Mar 1, 2026
Copilot finished work on behalf of lahma March 1, 2026 20:12
@lahma lahma marked this pull request as ready for review March 2, 2026 10:15
@lahma lahma merged commit e81f092 into master Mar 2, 2026
2 checks passed
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.

Sorting JSonSchema(4) in NSwag (Documentation request, and maybe feature)

2 participants