Commit 3850180
committed
fix: extend already-constructed Zod v4 schemas with .openapi()
Currently `extendZodWithOpenApi` only attaches `.openapi` to schemas
constructed AFTER it runs. Schemas constructed earlier in the import
graph crash at first use with `TypeError: <schema>.openapi is not a
function`.
Why this happens (Zod v4):
- `core.$constructor` does not link `ZodObject.prototype` (or any other
concrete schema class's prototype) to `ZodType.prototype`. They are
separate `_.prototype` objects — `ZodObject.prototype.__proto__ ===
Object.prototype`, not `ZodType.prototype`.
- During schema construction, `init` iterates `Object.keys(_.prototype)`
and BINDS each method onto the instance. So a method added to
`ZodType.prototype` after the schema is constructed is unreachable:
it's not on the instance, and it's not in the instance's prototype
chain (because the chain skips `ZodType.prototype`).
Repro: any ESM dependency graph where a schema module evaluates before
the consumer that calls `extendZodWithOpenApi`. We hit it consistently
in Next.js Turbopack production builds, where chunk layout caused
`@vcr/core/schemas` to evaluate before `@hono/zod-openapi` (which calls
`extendZodWithOpenApi(z)` on import). On macOS the page-data collector happened to instantiate the chunks in
an order that masked the issue; on the GitHub Linux runner it didn't.
Fix: after assigning `.openapi` to `ZodType.prototype`, mirror the same
function onto every concrete `Zod*.prototype`. Pre-existing instances
then resolve `.openapi` via prototype-chain lookup; newly constructed
instances resolve it via the same bind-on-init loop the `$constructor`
runs against the schema class's own prototype.
Adds `spec/late-extend.spec.ts` with three regression tests:
- `.openapi` exists on schemas constructed before the call
- `.openapi` still works on schemas constructed after the call
- end-to-end: a pre-existing schema produces a valid spec
All existing tests still pass (49 suites / 298 tests).1 parent 2fb24b0 commit 3850180
2 files changed
Lines changed: 98 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
237 | 237 | | |
238 | 238 | | |
239 | 239 | | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
240 | 258 | | |
241 | 259 | | |
242 | 260 | | |
| |||
0 commit comments