Skip to content

fix: bump zod-openapi to 5.4.6 with discriminated-union regression test (supersedes #151)#162

Merged
mcampa merged 3 commits into
masterfrom
repro/discriminated-union-crash
May 22, 2026
Merged

fix: bump zod-openapi to 5.4.6 with discriminated-union regression test (supersedes #151)#162
mcampa merged 3 commits into
masterfrom
repro/discriminated-union-crash

Conversation

@mcampa
Copy link
Copy Markdown
Owner

@mcampa mcampa commented May 22, 2026

Supersedes #151 by adding a regression test alongside the version bump.

Background

zod 4.1.13 changed how discriminated unions are serialized to JSON Schema — they now emit `anyOf` instead of `oneOf`. zod-openapi 5.4.0–5.4.3 still expected `oneOf` in its schema-override path, so calling `generateOpenApiDocument` on any router with a discriminated union input would crash:

```
TypeError: Cannot convert undefined or null to object
at Function.entries ()
at override (zod-openapi/dist/components-…cjs:231:39)
at JSONSchemaGenerator.override (…:398:4)
```

zod-openapi 5.4.4 fixed this (samchungy/zod-openapi#556). 5.4.5 added type-declaration fixes. 5.4.6 addresses additional Zod 4.3+ "cannot be represented in OpenAPI" compatibility issues.

Changes

  1. `test: reproduce zod-openapi discriminated union crash from Fix: update zod-openapi to 5.4.5 #151` — pins zod to a version where the bug manifests and adds a test that fails on master.
  2. `fix: bump zod-openapi to 5.4.6` — resolves the failing test. Also relaxes the zod devDep pin to `^4.1.13` so the regression test keeps exercising the previously-broken code path while allowing minor upgrades.

Two snapshots were regenerated because zod 4.1.x's `z.string().uuid()` regex now accepts the max UUID (`ffffffff-ffff-ffff-ffff-ffffffffffff`) — pure upstream change, no behavior shift in this package.

Test plan

  • Reproducer commit alone (zod-openapi 5.4.2 + zod 4.1.13): test fails with the exact stack trace from Fix: update zod-openapi to 5.4.5 #151
  • Fix commit applied: `pnpm exec jest` — 130/130 tests passing
  • `pnpm run build` succeeds (tsc + jest + cjs + esm)
  • CI green

🤖 Generated with Claude Code

mcampa and others added 2 commits May 22, 2026 09:06
Pin zod to 4.1.13 (where discriminated unions began emitting anyOf
instead of oneOf) and add a test that calls generateOpenApiDocument on a
router with a discriminated union input. With zod-openapi 5.4.2 the test
fails with "Cannot convert undefined or null to object" in
JSONSchemaGenerator.override, matching the stack trace in #151.

Verified the test passes after bumping zod-openapi to 5.4.5, which is
what #151 proposes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Resolves the crash demonstrated in the previous commit. zod-openapi 5.4.4
fixes the discriminated union override path (reading anyOf instead of
oneOf), 5.4.5 fixes type declarations, and 5.4.6 addresses additional
Zod 4.3+ "cannot be represented in OpenAPI" compatibility issues.

Also relaxes the zod devDep pin to ^4.1.13 so the test continues to
exercise the previously-broken code path while allowing forward minor
upgrades.

Snapshot update is upstream zod behavior: the v4.1 UUID regex now
includes the max UUID (ffffffff-ffff-ffff-ffff-ffffffffffff).

Supersedes #151.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mcampa mcampa force-pushed the repro/discriminated-union-crash branch from c3b650b to 8060c5b Compare May 22, 2026 16:07
@mcampa mcampa requested a review from Copilot May 22, 2026 16:08
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the development dependency stack (zod + zod-openapi) to avoid a known zod-openapi crash when generating OpenAPI docs for routers whose inputs contain Zod discriminated unions, and adds a regression test to ensure document generation remains stable for that case.

Changes:

  • Bump zod-openapi devDependency to 5.4.6 and zod devDependency to ^4.1.13.
  • Add a regression test ensuring generateOpenApiDocument does not throw when a procedure input includes a discriminated union.
  • Regenerate affected Jest snapshots due to upstream Zod UUID-regex changes.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

File Description
test/generator.test.ts Updates snapshots and adds a discriminated-union crash regression test around generateOpenApiDocument.
package.json Bumps devDependencies for zod and zod-openapi to versions that include the upstream crash fix.
pnpm-lock.yaml Updates the lockfile to reflect the new zod and zod-openapi versions.
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread package.json
5.4.0–5.4.3 crash when generating OpenAPI docs for routers with
discriminated union inputs on zod >=4.1.13 (see #151). Tightening the
peer dep prevents consumers from installing a known-broken range.

Addresses Copilot review on #162.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mcampa
Copy link
Copy Markdown
Owner Author

mcampa commented May 22, 2026

Addressed in eda684b — raised the zod-openapi peerDependency to ^5.4.4 so consumers can't install the known-broken 5.4.0–5.4.3 range that crashes on discriminated unions with zod ≥4.1.13.

@mcampa mcampa merged commit 58ab912 into master May 22, 2026
3 checks passed
@mcampa mcampa deleted the repro/discriminated-union-crash branch May 22, 2026 16:14
@mcampa mcampa mentioned this pull request May 22, 2026
4 tasks
mcampa added a commit that referenced this pull request May 22, 2026
- feat: contact/license fields on GenerateOpenApiDocumentOptions (#157)
- feat: support zod v3 (>=3.25) alongside v4 via zod/v4 permalink (#161)
- fix: bump zod-openapi to 5.4.6 and raise peer dep to ^5.4.4 to prevent
  the discriminated-union crash on zod >=4.1.13 (#162)
- fix: add x-${string} index signature to contact/license types (#160)
- chore: bump h3 to ^1.15.5 (#159)

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants