fix(zod): correct inline reusable schema emission (missing import + dropped defs) - #3468
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (4)
📝 WalkthroughWalkthroughSeeds reusable Zod schemas from raw ChangesInline Zod Reusable Schemas
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Fixes regressions in inline reusable Zod schema generation by correctly seeding component schema refs from raw OpenAPI keys, and by ensuring the generated output includes exactly one necessary Zod import even when operations are pure $ref aliases.
Changes:
- Seed inline reusable schema refs from raw
components.schemaskeys to avoid dropping schemas with sanitized identifiers. - Adjust
includeZodImportlogic to depend on whether operations actually use thezodidentifier. - Add targeted regression tests for sanitized-name schemas and for pure-
$refoperations missing Zod imports.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| packages/orval/src/write-zod-specs.ts | Seeds refs from raw component schema keys to prevent silently dropped schemas. |
| packages/orval/src/write-zod-specs.test.ts | Adds regression test covering raw-name vs sanitized-name mismatch. |
| packages/orval/src/write-specs.ts | Makes Zod import emission conditional on whether operations reference zod. |
| packages/orval/src/generate-spec.test.ts | Adds end-to-end regression test ensuring exactly one zod import for pure-$ref operations. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
looks like Conflicts now |
…ropped defs) Two pre-existing bugs in the inline single-file reusable-schema writer (`client: 'zod'` + `generateReusableSchemas`, no `schemas:` dir): 1. Missing `import * as zod from 'zod'`. The zod client's import is a usage-gated dependency import — emitted only when an operation's generated schema references the `zod` token. When every operation is a pure-`$ref` alias (`export const FooResponse = Bar`), the client emits no zod import, and the inline block also skipped it (it keyed off `!hasOperations`), leaving the generated schemas referencing an undefined `zod`. Now the inline block supplies the import unless an operation actually uses zod. 2. Dropped definitions for sanitized names. The inline writer seeded reusable refs from `builder.schemas`, whose names are the *sanitized* model identifiers (`__schema0` -> `_Schema0`). The resulting ref (`#/components/schemas/_Schema0`) doesn't exist in `components.schemas`, so the definition was silently dropped whenever the schema was reachable only from operations (it survived when another component referenced it, via transitive expansion). Now it seeds from the raw `components.schemas` keys, mirroring the per-file writer (`writeZodSchemasReusable`). Both verified to type-check under strict on zod v3 and v4. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Match /from ['"]zod['"]/ so the single-import assertion survives generator quote-style changes while still asserting exactly one zod module import. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
b21c699 to
36cee17
Compare
Summary
Two pre-existing bugs in the inline single-file reusable-schema writer (
client: 'zod'+override.zod.generateReusableSchemas, with noschemas:directory). Both produce output that does not compile. Found while working on #3467; fixed here independently.1. Missing
import * as zod from 'zod'The zod client's import is a usage-gated dependency import — emitted only when an operation's generated schema references the
zodtoken. When every operation is a pure-$refalias, e.g.the client emits no zod import. The inline schema block also skipped it (it keyed off
!hasOperations), so the file ended up referencingzodwith no import at all.Now the inline block supplies the import unless an operation actually references
zod(mirroring the client's own gate), so there is always exactly one zod import.2. Dropped definitions for sanitized names
The inline writer seeded reusable refs from
builder.schemas, whosenameis the sanitized model identifier (__schema0→_Schema0). The resulting ref#/components/schemas/_Schema0doesn't exist incomponents.schemas(the real key is__schema0), so the definition was silently dropped whenever the schema was reachable only from operations — leaving the operation wrapper that references it dangling. (It survived when another component schema referenced it, via transitive expansion, which masked the bug.)Now it seeds from the raw
components.schemaskeys, exactly like the per-file writerwriteZodSchemasReusablealready does (see its comment, added in #3465).Test plan
write-zod-specs):generateZodSchemasInlineemits a schema whose raw name needs sanitizing (__my_data→_MyData)generate-spec): a spec whose only operation is a pure-$refalias yields exactly onefrom 'zod'import (previously zero)zod) still asserts exactly one import — no double-import regressionstricton zod v3 and v4 for both scenarioslint,typecheck, and fullorvalsuite (119 tests) pass🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
$refresponses).Tests
$refoperations and correct Zod import behavior.