Skip to content

fix: escape JSON Pointer tokens in toJSONSchema $ref (RFC 6901)#6051

Open
godfengliang wants to merge 1 commit into
colinhacks:mainfrom
godfengliang:fix-json-schema-ref-encoding
Open

fix: escape JSON Pointer tokens in toJSONSchema $ref (RFC 6901)#6051
godfengliang wants to merge 1 commit into
colinhacks:mainfrom
godfengliang:fix-json-schema-ref-encoding

Conversation

@godfengliang

Copy link
Copy Markdown

Fixes #6027

Problem

When a schema id contains / or ~ characters, the $ref URI fragment was not being escaped per RFC 6901 (JSON Pointer):

  • ~ should become ~0
  • / should become ~1

For example, with id Shared/User~, the $ref was:
#/$defs/Shared/User~ (incorrect)

Now correctly produces:
#/$defs/Shared~1User~0

The $defs object key remains unescaped (as it's a plain JSON object key, not a JSON Pointer).

Fix

Apply split("~").join("~0").split("/").join("~1") to the defId in both $ref construction paths in extractDefs:

  1. The shared schema path (line ~263)
  2. The self-contained schema path (line ~274)

Testing

Verified with three test cases:

  • id: "Shared/User~" -> $ref: "#/$defs/Shared~1User~0"
  • id: "MySchema" -> $ref: "#/$defs/MySchema" ✓ (no change for normal ids)
  • id: "My~Schema" -> $ref: "#/$defs/My~0Schema"

@pullfrog

pullfrog Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Run failed. View the logs →

Pullfrog  | Rerun failed job ➔View workflow run | via Pullfrog | Using Kimi K2𝕏

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.

Bug: toJSONSchema $ref pointer not encoded when meta id contains / or ~

1 participant