Commit 411f6c6
fix(v4): resolve stack overflow in toJSONSchema for recursive lazy with describe (#5797)
* fix(v4): resolve stack overflow in toJSONSchema for recursive lazy with describe
When .describe() or .meta() is called on a recursive z.lazy() schema,
the clone re-evaluates the getter on each recursion level, producing
unbounded new schema objects. Walk up to the root lazy to reuse its
cached innerType instead.
Fixes #5777.
* fix(v4): cache lazy innerType on def so clones share identity
Move the recursion fix from the JSON-schema processor into the $ZodLazy
constructor. Caching the resolved inner type on the shared `def` means
all clones of a lazy (via .describe(), .meta(), etc.) resolve to the
same inner instance, so cycle detection works for any consumer — not
just toJSONSchema — and parsing no longer rebuilds a fresh subtree on
every access through a clone.
---------
Co-authored-by: Colin McDonnell <[email protected]>1 parent f457edf commit 411f6c6
2 files changed
Lines changed: 23 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3029 | 3029 | | |
3030 | 3030 | | |
3031 | 3031 | | |
| 3032 | + | |
| 3033 | + | |
| 3034 | + | |
| 3035 | + | |
| 3036 | + | |
| 3037 | + | |
| 3038 | + | |
| 3039 | + | |
| 3040 | + | |
| 3041 | + | |
| 3042 | + | |
| 3043 | + | |
| 3044 | + | |
| 3045 | + | |
| 3046 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4404 | 4404 | | |
4405 | 4405 | | |
4406 | 4406 | | |
4407 | | - | |
4408 | | - | |
4409 | | - | |
4410 | | - | |
4411 | | - | |
4412 | | - | |
4413 | | - | |
4414 | | - | |
| 4407 | + | |
| 4408 | + | |
| 4409 | + | |
| 4410 | + | |
| 4411 | + | |
| 4412 | + | |
| 4413 | + | |
| 4414 | + | |
4415 | 4415 | | |
4416 | 4416 | | |
4417 | 4417 | | |
| |||
0 commit comments