Skip to content

Json schema exporter doesn't percent-encode $ref #130162

Description

@Youssef1313

Description

When creating a schema, my understanding for the $ref part is that it follows https://www.rfc-editor.org/info/rfc6901/#section-6 (The URI Fragment Identifier Representation part)

Looking at the example there, the reference tokens appear to be percent-encoded, but STJ doesn't encode it.

Reproduction Steps

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Schema;
using System.Text.Json.Serialization;

var options = new JsonSerializerOptions()
{
    TypeInfoResolver = JsonSerializerOptions.Default.TypeInfoResolver,
    WriteIndented = true,
};

Console.WriteLine(JsonSchemaExporter.GetJsonSchemaAsNode(options, typeof(Base))
    .ToJsonString(options));

[JsonPolymorphic(TypeDiscriminatorPropertyName = "$type")]
[JsonDerivedType(typeof(Derived1), typeDiscriminator: "alpha")]
[JsonDerivedType(typeof(Derived2), typeDiscriminator: "beta")]
internal abstract class Base
{
    [JsonPropertyName("hello%20world")]
    public required Dictionary<string, string[]> SpecialChars { get; set; }

}

internal class Derived1 : Base { }
internal class Derived2 : Base { }

Expected behavior

{
  "type": [
    "object",
    "null"
  ],
  "required": [
    "$type"
  ],
  "anyOf": [
    {
      "properties": {
        "$type": {
          "const": "alpha"
        },
        "hello%20world": {
          "type": "object",
          "additionalProperties": {
            "type": [
              "array",
              "null"
            ],
            "items": {
              "type": [
                "string",
                "null"
              ]
            }
          }
        }
      },
      "required": [
        "hello%20world"
      ]
    },
    {
      "properties": {
        "$type": {
          "const": "beta"
        },
        "hello%20world": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/anyOf/0/properties/hello%2520world/additionalProperties"
          }
        }
      },
      "required": [
        "hello%20world"
      ]
    }
  ]
}

Actual behavior

{
  "type": [
    "object",
    "null"
  ],
  "required": [
    "$type"
  ],
  "anyOf": [
    {
      "properties": {
        "$type": {
          "const": "alpha"
        },
        "hello%20world": {
          "type": "object",
          "additionalProperties": {
            "type": [
              "array",
              "null"
            ],
            "items": {
              "type": [
                "string",
                "null"
              ]
            }
          }
        }
      },
      "required": [
        "hello%20world"
      ]
    },
    {
      "properties": {
        "$type": {
          "const": "beta"
        },
        "hello%20world": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/anyOf/0/properties/hello%20world/additionalProperties"
          }
        }
      },
      "required": [
        "hello%20world"
      ]
    }
  ]
}

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

See https://www.jsonschemavalidator.net/s/DC2SIxpb (with the currently produced schema showing as invalid):

Could not resolve schema reference '#/anyOf/0/properties/hello%20world/additionalProperties'. Path 'anyOf[1].properties.hello%20world.additionalProperties', line 42, position 35.

See https://www.jsonschemavalidator.net/s/jBmPEngV (with the expected schema showing as valid).

Activity

  1. added
    untriaged
    on Jul 3, 2026
  2. dotnet-policy-service commented on Jul 3, 2026

    @dotnet-policy-service
    Contributor

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

  3. added theissue type on Jul 3, 2026
  4. removed
    untriaged
    on Jul 17, 2026
  5. locked and limited conversation to collaborators on Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions