Skip to content

fix: strip redundant id from $defs entries in toJSONSchema#5759

Open
mibragimov wants to merge 1 commit intocolinhacks:mainfrom
mibragimov:fix/to-json-schema-meta-id
Open

fix: strip redundant id from $defs entries in toJSONSchema#5759
mibragimov wants to merge 1 commit intocolinhacks:mainfrom
mibragimov:fix/to-json-schema-meta-id

Conversation

@mibragimov
Copy link
Copy Markdown

Problem

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 via $ref. However, the id was also being included verbatim in the definition body, producing redundant output:

{
  "$defs": {
    "Inner": {
      "id": "Inner",
      "type": "string"
    }
  }
}

The presence of id inside $defs entries is misleading — it looks like a JSON Schema $id, but it's just Zod's internal id tag. The id is already encoded as the $defs key itself, so including it in the value is redundant.

Fixes #5731

Solution

In extractToDef(), after copying the schema into seen.def, delete the id property from the definition body when it matches defId — the id is already encoded as the $defs key 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.

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.
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.

z.toJSONSchema: .meta({ id }) leaks id into $defs entries — should use $id or omit it

1 participant