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
fix(zod): emit recursive reusable schemas with full TypeScript types (orval-labs#3467)
* fix(zod): emit recursive reusable schemas with full TypeScript types
A self-referential reusable schema is generated as a `const X = ...zod.lazy(
() => X)...` that reads its own binding inside its initializer. Under strict /
noImplicitAny TypeScript rejects this with TS7022 ("'X' implicitly has type
'any' ... referenced directly or indirectly in its own initializer").
Detect schemas that sit in a cycle (SCC > 1 or a self-loop) and, for those,
generate the recursive TS type with orval's own model generator (`resolveValue`,
the same path that produces `export type X` in the model output, so identifiers
line up via `getRefInfo`) and pin the schema to it:
export type X = ...X[]...;
export const X: zod.ZodType<X> = zod.union([...zod.lazy(() => X)...]);
The annotation both satisfies the compiler and preserves full `z.infer` typing
through the recursion, instead of collapsing recursive positions to `unknown`.
Acyclic schemas are unchanged (still derive `zod.input<typeof X>`). Applies to
both the inline single-file and per-file reusable writers; verified on zod v3
and v4 incl. mutual recursion, coercion + defaults, and sanitized names.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(zod): address review on recursive reusable schemas
- resolve the recursive schema lookup key via getRefInfo/isComponentRef
instead of a blind ref slice (decodes JSON Pointer escapes, guards the
#/components/schemas/ prefix before indexing components.schemas)
- make the recursive-type assertion whitespace-tolerant (regex)
- drop the Record<string, unknown> cast in reusable-schema tests; the
typed override.zod shape already carries generateReusableSchemas
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
0 commit comments