Skip to content

fix(zod): fall back to z.union() when discriminated union members are ZodIntersection#3813

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-typescript-errors-zod-schemas
Draft

fix(zod): fall back to z.union() when discriminated union members are ZodIntersection#3813
Copilot wants to merge 2 commits intomainfrom
copilot/fix-typescript-errors-zod-schemas

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 22, 2026

Calling .extend() on a ZodIntersection (produced by allOf schemas) is a TypeScript error. When a discriminated union's member schemas use allOf inheritance, the generated z.discriminatedUnion(...).extend(...) pattern fails to type-check.

Before (broken):

// zUserNotificationDeleteContentRunning = zBase.and(z.object({...})) — ZodIntersection
export const zUserNotificationDeleteContent = z.discriminatedUnion('status', [
    zUserNotificationDeleteContentRunning.extend({ status: z.literal('running') }), // ❌ TS error
    ...
]);

After (fixed):

export const zUserNotificationDeleteContent = z.union([
    z.object({ status: z.literal('running') }).and(zUserNotificationDeleteContentRunning),
    ...
]);

Changes

  • shared/types.ts — add isIntersection: boolean to ZodMeta
  • shared/meta.ts — include isIntersection: false in defaultMeta / composeMeta
  • v3/v4/mini walker.ts — set isIntersection: true in the intersection() handler; add isIntersection: false to manually constructed union meta objects
  • v3/v4/mini processor.ts — merge ctx.meta (carries resource/resourceId) with { isIntersection: true } when exporting intersection schemas, so the flag is stored on the registered symbol
  • shared/discriminated-union.ts — before calling .extend(), query the ref symbol's meta; if isIntersection === true, return null to trigger z.union() fallback
  • Test specs & snapshots — new discriminator-allof-member.yaml specs for OpenAPI 3.0.x and 3.1.x with corresponding snapshots asserting the z.union() output

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • fonts.googleapis.com
    • Triggering command: /opt/hostedtoolcache/node/24.15.0/x64/bin/node /opt/hostedtoolcache/node/24.15.0/x64/bin/node /home/REDACTED/work/openapi-ts/openapi-ts/node_modules/.pnpm/[email protected][email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/jest-worker/processChild.js packages/shared/nuxt-module-build build /systemd-executor dules/.bin/../vue-tsc/bin/vue-tsc.js submodules | head -n 10 packages/shared/src/openApi/shared/transforms/index.ts cal/bin/bash packages/shared/sh packages/shared/-c packages/shared/next build bash --no�� --noprofile napi-ts/packages/openapi-ts/src/plugins/ tools/pnpm/10.33.0/bin/node (dns block)
    • Triggering command: /opt/hostedtoolcache/node/24.15.0/x64/bin/node /opt/hostedtoolcache/node/24.15.0/x64/bin/node /home/REDACTED/work/openapi-ts/openapi-ts/node_modules/.pnpm/[email protected][email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/jest-worker/processChild.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@bolt-new-by-stackblitz
Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 22, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
hey-api-docs Ready Ready Preview, Comment Apr 22, 2026 7:27pm

Request Review

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 22, 2026

⚠️ No Changeset found

Latest commit: 03003e4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pullfrog
Copy link
Copy Markdown
Contributor

pullfrog Bot commented Apr 22, 2026

TL;DR — This PR is a WIP placeholder with no code changes yet. It was opened by Copilot to address #3811, which reports TypeScript errors when using .extend() on z.object({}).and() in generated Zod schemas (introduced in v0.96.1 via #3780). No files have been modified.

Key changes

  • (No code changes yet — the PR contains only an initial plan commit with 0 files changed)

Summary | 0 files | 1 commit | base: maincopilot/fix-typescript-errors-zod-schemas

Pullfrog  | View workflow run | via Pullfrog𝕏

Copy link
Copy Markdown
Contributor

@pullfrog pullfrog Bot left a comment

Choose a reason for hiding this comment

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

This is a draft WIP PR with zero code changes — the only commit ("Initial plan") contains no file modifications. There is nothing to review yet. Will re-review once implementation commits are pushed.

Task list (3/3 completed)
  • Read the PR diff and metadata
  • Analyze the PR content and identify issues
  • Submit review

Pullfrog  | Fix it ➔View workflow run𝕏

… intersections

When discriminated union members have allOf schemas, they generate ZodIntersection
which does not support .extend(). Detect this case by storing isIntersection: true
in the symbol meta and fall back to z.union() in tryBuildDiscriminatedUnion.

- Add isIntersection field to ZodMeta type
- Set isIntersection: true in intersection() handler of all three walkers
- Set isIntersection: false in union() handler (manual meta) of all three walkers
- Merge ctx.meta with isIntersection flag in processor exportAst calls
- Check querySymbol meta for isIntersection in tryBuildDiscriminatedUnion
- Add test specs and snapshots for the discriminator-allof-member scenario

Agent-Logs-Url: https://github.com/hey-api/openapi-ts/sessions/1c6f992d-8feb-49c9-9cc6-7830259cfe5c
Copilot AI changed the title [WIP] Fix TypeScript errors in generated Zod schemas fix(zod): fall back to z.union() when discriminated union members are ZodIntersection Apr 22, 2026
Copilot AI requested a review from mrlubos April 22, 2026 19:27
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 22, 2026

Codecov Report

❌ Patch coverage is 0% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 39.82%. Comparing base (9abd347) to head (03003e4).

Files with missing lines Patch % Lines
...kages/openapi-ts/src/plugins/zod/mini/processor.ts 0.00% 2 Missing ⚠️
...i-ts/src/plugins/zod/shared/discriminated-union.ts 0.00% 0 Missing and 2 partials ⚠️
...ackages/openapi-ts/src/plugins/zod/v3/processor.ts 0.00% 2 Missing ⚠️
...ackages/openapi-ts/src/plugins/zod/v4/processor.ts 0.00% 2 Missing ⚠️
packages/openapi-ts/src/plugins/zod/shared/meta.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3813      +/-   ##
==========================================
- Coverage   39.84%   39.82%   -0.02%     
==========================================
  Files         530      530              
  Lines       19475    19481       +6     
  Branches     5802     5814      +12     
==========================================
  Hits         7759     7759              
- Misses       9485     9489       +4     
- Partials     2231     2233       +2     
Flag Coverage Δ
unittests 39.82% <0.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 22, 2026

Open in StackBlitz

@hey-api/codegen-core

npm i https://pkg.pr.new/@hey-api/codegen-core@3813

@hey-api/json-schema-ref-parser

npm i https://pkg.pr.new/@hey-api/json-schema-ref-parser@3813

@hey-api/nuxt

npm i https://pkg.pr.new/@hey-api/nuxt@3813

@hey-api/openapi-ts

npm i https://pkg.pr.new/@hey-api/openapi-ts@3813

@hey-api/shared

npm i https://pkg.pr.new/@hey-api/shared@3813

@hey-api/spec-types

npm i https://pkg.pr.new/@hey-api/spec-types@3813

@hey-api/types

npm i https://pkg.pr.new/@hey-api/types@3813

@hey-api/vite-plugin

npm i https://pkg.pr.new/@hey-api/vite-plugin@3813

commit: 03003e4

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.

v0.96.1 cause typescript errors in generated zod schemas because of extend() on z.object({}).and()

2 participants