fix(zod): sanitize extracted definition refs#1903
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6ad9563f79
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const name = item.path | ||
| .slice(refs.basePath.length + 1) | ||
| .join('_') | ||
| .replace(/\s+/g, '_'); |
There was a problem hiding this comment.
Preserve unique names when normalizing refs
When a schema contains two distinct reused definitions whose extracted paths differ only by whitespace vs underscores, this normalization maps both to the same definition name. For example, reused properties named Thing With Spaces and Thing_With_Spaces now both emit refs ending in Thing_With_Spaces, so the later refs.definitions[name] = item.def overwrites the earlier definition and existing $refs can resolve to the wrong schema. The sanitizer needs to escape or otherwise disambiguate normalized names instead of using a lossy replacement.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in d3c808dc: extracted definition path parts now encode non-alphanumeric characters instead of replacing whitespace with underscores, so Thing With Spaces and Thing_With_Spaces stay distinct. The test now covers both names.
Summary
_when zod v3 schemas extract reused definitions to root refs$refvalues anddefinitionskeys aligned after normalizationWhy
zodResponseFormatcan currently produce$refvalues such as#/definitions/example-scope_properties_group_properties_Thing With Spaces. Those literal spaces can be rejected by the API and make the generated schema harder to consume.Fixes #1679.
Testing
npx -y [email protected] test tests/helpers/zod.test.ts --runInBandnpx -y [email protected] prettier --check src/_vendor/zod-to-json-schema/parseDef.ts tests/helpers/zod.test.tsnpx -y [email protected] eslint src/_vendor/zod-to-json-schema/parseDef.ts tests/helpers/zod.test.tsnpx -y [email protected] buildgit diff --check