Skip to content

fix(zod): preserve contextual types for reusable reference defaults - #4441

Merged
melloware merged 4 commits into
orval-labs:masterfrom
irontaek:fix/zod-reusable-default-types
Nov 5, 2026
Merged

melloware merged 4 commits into
orval-labs:masterfrom
irontaek:fix/zod-reusable-default-types

Conversation

@irontaek

@irontaek irontaek commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4437. Fixes #4438.

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.

@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

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/**
Run configuration

Commits

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.

Changes: Reusable schema defaults

Layer / File(s) Summary
Inline composite defaults and validation
packages/zod/src/index.ts, packages/zod/src/reusable-defaults.test.ts, packages/zod/src/zod.test.ts, tests/specifications/issue-4255.yaml, tests/reusable-zod-defaults.spec.ts, tests/vitest.snapshots.ts
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.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

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: Passed 5

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 Passed 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 Passed 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 Passed 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.

Share

Recent review info

Run configuration

Commits

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.

@melloware melloware added the zod Zod schema client related issue label Nov 3, 2026

@coderabbitai coderabbitai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Fix comments: Fix CodeRabbit comments on this PR

Comment thread packages/zod/src/index.ts Resolved

@melloware melloware left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See AI feedback

@pkg-pr-new

pkg-pr-new Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@orval/angular

bun add https://pkg.pr.new/orval-labs/orval/@orval/angular@510087f

@orval/axios

bun add https://pkg.pr.new/orval-labs/orval/@orval/axios@510087f

@orval/core

bun add https://pkg.pr.new/orval-labs/orval/@orval/core@510087f

@orval/effect

bun add https://pkg.pr.new/orval-labs/orval/@orval/effect@510087f

@orval/fetch

bun add https://pkg.pr.new/orval-labs/orval/@orval/fetch@510087f

@orval/hono

bun add https://pkg.pr.new/orval-labs/orval/@orval/hono@510087f

@orval/mcp

bun add https://pkg.pr.new/orval-labs/orval/@orval/mcp@510087f

@orval/mock

bun add https://pkg.pr.new/orval-labs/orval/@orval/mock@510087f

orval

bun add https://pkg.pr.new/orval-labs/orval@510087f

@orval/pinia-colada

bun add https://pkg.pr.new/orval-labs/orval/@orval/pinia-colada@510087f

@orval/query

bun add https://pkg.pr.new/orval-labs/orval/@orval/query@510087f

@orval/solid-start

bun add https://pkg.pr.new/orval-labs/orval/@orval/solid-start@510087f

@orval/swr

bun add https://pkg.pr.new/orval-labs/orval/@orval/swr@510087f

@orval/valibot

bun add https://pkg.pr.new/orval-labs/orval/@orval/valibot@510087f

@orval/zod

bun add https://pkg.pr.new/orval-labs/orval/@orval/zod@510087f

commit: 510087f

@melloware melloware left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like build is failing

@melloware
melloware merged commit 5b03e81 into orval-labs:master Nov 5, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

zod Zod schema client related issue

Projects

None yet

2 participants