You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(zod): preserve contextual types for reusable reference defaults - #4441
With generateReusableSchemas: true, a $ref with an array/object default takes the named-reference path and hoists the default into an untyped constant. TypeScript widens tuples to arrays and enum-valued object properties to string, so the generated .default(...) call fails with TS2769 in both Zod 3 and Zod 4.
Keep composite defaults inline in that path so the referenced schema provides their contextual type, consistent with the existing inline-default treatment in #4036 and #4257. Object defaults use a spread so undeclared default keys remain accepted without excess-property checks; nested tuples and enum properties retain their contextual types. Reference-like text in inline defaults is emitted with an escaped sentinel prefix, preserving literal string values and object keys through reference rewriting. A generated-code runtime regression covers both. Scalar defaults retain their existing constants. Generated-output change: composite $ref defaults no longer emit a separate *Default export.
Added regression cases for both Zod versions, including undeclared default keys, and extended the CLI regression fixture with direct tuple references and an enum-object default containing a nested tuple and an extra key. Updated the generated schema and its snapshot.
Validation:
Actual generated-code compilation: tuple/object cases fail with TS2769 on the original generator for both Zod 3 and 4; all four combinations compile after the fix.
Full package unit tests: 6,208 pass.
API-generation snapshots and the new generated-code runtime regression: 7,952 pass.
All package release builds and package typechecks pass.
Repository formatting, package lint and vp check for the changed source/tests pass.
Regenerated Zod, Axios and reusable SWR/Zod outputs; the expanded generated schema also passes strict TypeScript compilation.
Framework sample runtime suites and Windows execution were not run locally.
AI assistance: Codex was used to investigate, implement and verify this change.
Summary by CodeRabbit
Bug Fixes
Defaults for reusable schemas containing objects or arrays are now preserved in generated Zod output, including their contextual values. Primitive default handling remains unchanged. Validation confirms that parsed results retain configured object and array defaults, including additional object properties, without changing the behavior of existing primitive defaults.
Reviewing files that changed from the base of the PR and between 529a760 and 510087f.
Files ignored due to path filters: 2
tests/__snapshots__/zod/issue-4255/schemas.zod.ts is excluded by !**/__snapshots__/**
tests/generated/zod/issue-4255/schemas.zod.ts is excluded by !**/generated/**
You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.
Use the checkbox below for a quick retry:
Trigger review
No actionable comments were generated in the recent review.
Walkthrough
Object and array defaults on reusable $ref schemas are now emitted inline in generated Zod output. Other default values retain the named-constant path. Tests and a specification example cover tuple defaults and enum-valued object defaults.
Object defaults are spread inline, and array defaults remain array literals. The generated literals escape __REF_ before sentinel rewriting. Tests cover inline output for both Zod versions and parsed default values.
orval-labs/orval#4406: Both PRs change default emission in the reusable $ref sibling path.
Suggested reviewers:melloware
Merge Risk: Minimal · previous review 529a7
The change preserves contextual typing for reusable composite defaults and adds regression coverage. No merge-blocking issue was identified.
Pre-merge checks: 5
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title names the fix, the affected area (zod), and the core behavior change: composite defaults on reusable $ref schemas stay inline so the referenced schema supplies contextual types. The raw summ…
Linked Issues check
Both linked issues are direct and open. Both have coding requirements. No non-coding requirement applies.
[#4437, #4438] Requirement: with generateReusableSchemas: true, Zod output must keep a compos…
Out of Scope Changes check
All reviewed changes relate to #4437 and #4438.
- packages/zod/src/index.ts: the reusable-reference default path. This is the fix itself. - packages/zod/src/reusable-defaults.test.ts and packages/zod…
Docstring Coverage
No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Title check
Explanation
The title names the fix, the affected area (zod), and the core behavior change: composite defaults on reusable $ref schemas stay inline so the referenced schema supplies contextual types. The raw summary and PR objectives both describe this change as the main one, so the title is specific and matches the changeset.
Full details: Linked Issues check
Explanation
Both linked issues are direct and open. Both have coding requirements. No non-coding requirement applies.
[#4437, #4438] Requirement: with generateReusableSchemas: true, Zod output must keep a composite $ref default (tuple or object) typed against the referenced schema. The generated default must compile without manual annotation.
Implementation: packages/zod/src/index.ts, applyChainableSiblings. Array defaults on $ref schemas are emitted as inline literals. Object defaults are emitted as spreads into a fresh object literal. Both take their contextual type from the referenced schema, so tuple types (#4437) and enum literal types (#4438) are preserved. Scalar defaults keep their named constants. The PR description states that this matches the existing inline-default treatment from #4036 and #4257. Reference-like text in inline defaults is escaped with a sentinel prefix, so literal string values and object keys survive reference rewriting.
Automated tests: packages/zod/src/reusable-defaults.test.ts covers tuple and enum-object defaults for Zod 3 and Zod 4, including an object with an extra undeclared key. packages/zod/src/zod.test.ts expects inline composite defaults and no separate const. tests/specifications/issue-4255.yaml adds a direct tuple reference (default [1, 300]) and an enum-object reference with a nested tuple and an extra key. tests/reusable-zod-defaults.spec.ts is a runtime test that checks parsed output, including escaped _REF keys.
Changes since the previous review: the object spread and the escaped _REF handling are reflected in the current summary. The earlier conclusion for the inline-default mechanism still holds.
Limits: no read-only inspection tool was available to this assessment. The source diff was not read directly. The generated files tests/generated/zod/issue-4255/schemas.zod.ts and tests/snapshots/zod/issue-4255/schemas.zod.ts are in ignored_files and were not inspected. The TS2769 failures on the original generator and the four-combination compile results are author-reported and were not re-run. The conclusion relies on the change summary, the test descriptions, and the PR description.
Full details: Out of Scope Changes check
Explanation
All reviewed changes relate to #4437 and #4438.
packages/zod/src/index.ts: the reusable-reference default path. This is the fix itself.
packages/zod/src/reusable-defaults.test.ts and packages/zod/src/zod.test.ts: regression coverage and updated expectations for inline composite defaults. These support the fix.
tests/specifications/issue-4255.yaml: extends the existing CLI fixture with the tuple and enum-object reference cases from both issues. This supports both issues.
tests/reusable-zod-defaults.spec.ts and tests/vitest.snapshots.ts: runtime regression for the generated code, and registration of that spec in the default Vitest project. These support the fix.
The removal of the separate *Default export for composite $ref defaults is a direct effect of the fix. The PR description discloses it as a generated-output change. Code that imports those names may need updates. This is not unrelated work.
The regenerated Axios and SWR/Zod outputs, and the ignored snapshot and generated files, were not inspected. No evidence shows unrelated content in them.
Full details: Docstring Coverage
Explanation
No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 5 files. (1 skipped: 1 unsupported.)
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Tip
🎯 Customize your risk assessments
Guide how CodeRabbit assesses merge risk and security architecture risk. Highlight sensitive changes and explain your security boundaries using Merge Risk Instructions and Security Risk Instructions in your review settings. Security architecture risk assessments require the Advanced plan or above.
✅ Automatic approvals, with your rules
Let CodeRabbit approve pull requests once required threads are resolved, pre-merge checks pass, and your risk limits are met. Enable Automatic approvals in review settings, with overrides for teams or file paths.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Reviewing files that changed from the base of the PR and between 699b93c and 529a760.
Files ignored due to path filters: 2
tests/__snapshots__/zod/issue-4255/schemas.zod.ts is excluded by !**/__snapshots__/**
tests/generated/zod/issue-4255/schemas.zod.ts is excluded by !**/generated/**
Files selected for processing: 4
packages/zod/src/index.ts
tests/reusable-zod-defaults.spec.ts
tests/specifications/issue-4255.yaml
tests/vitest.snapshots.ts
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
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
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.
Fixes #4437. Fixes #4438.
With
generateReusableSchemas: true, a$refwith an array/object default takes the named-reference path and hoists the default into an untyped constant. TypeScript widens tuples to arrays and enum-valued object properties tostring, so the generated.default(...)call fails with TS2769 in both Zod 3 and Zod 4.Keep composite defaults inline in that path so the referenced schema provides their contextual type, consistent with the existing inline-default treatment in #4036 and #4257. Object defaults use a spread so undeclared default keys remain accepted without excess-property checks; nested tuples and enum properties retain their contextual types. Reference-like text in inline defaults is emitted with an escaped sentinel prefix, preserving literal string values and object keys through reference rewriting. A generated-code runtime regression covers both. Scalar defaults retain their existing constants. Generated-output change: composite
$refdefaults no longer emit a separate*Defaultexport.Added regression cases for both Zod versions, including undeclared default keys, and extended the CLI regression fixture with direct tuple references and an enum-object default containing a nested tuple and an extra key. Updated the generated schema and its snapshot.
Validation:
vp checkfor the changed source/tests pass.Framework sample runtime suites and Windows execution were not run locally.
AI assistance: Codex was used to investigate, implement and verify this change.
Summary by CodeRabbit