fix: strip redundant id from $defs entries in toJSONSchema#5759
Open
mibragimov wants to merge 1 commit intocolinhacks:mainfrom
Open
fix: strip redundant id from $defs entries in toJSONSchema#5759mibragimov wants to merge 1 commit intocolinhacks:mainfrom
mibragimov wants to merge 1 commit intocolinhacks:mainfrom
Conversation
Fixes colinhacks#5731 When a schema is annotated with .meta({ id: "Name" }), the id is used as the key in $defs so that other schemas can reference it. However, the id was also included verbatim in the definition body, producing redundant (and invalid) output like: "$defs": { "Inner": { "id": "Inner", ← redundant "type": "string" } } In extractToDef(), after copying the schema into seen.def, delete id from the definition body when it matches the defId — the id is already encoded as the $defs key name.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a schema is annotated with
.meta({ id: "Name" }), theidis used as the key in$defsso that other schemas can reference it via$ref. However, theidwas also being included verbatim in the definition body, producing redundant output:{ "$defs": { "Inner": { "id": "Inner", "type": "string" } } }The presence of
idinside$defsentries is misleading — it looks like a JSON Schema$id, but it's just Zod's internalidtag. Theidis already encoded as the$defskey itself, so including it in the value is redundant.Fixes #5731
Solution
In
extractToDef(), after copying the schema intoseen.def, delete theidproperty from the definition body when it matchesdefId— the id is already encoded as the$defskey name.Before:
{ "$defs": { "Inner": { "id": "Inner", "type": "string" } } }After:
{ "$defs": { "Inner": { "type": "string" } } }Tests
Updated 6 existing snapshot tests that documented the old (buggy) behavior to reflect the correct output.