fix(zod): fall back to z.union for discriminated unions with empty-object members#3915
Open
pullfrog[bot] wants to merge 1 commit into
Open
fix(zod): fall back to z.union for discriminated unions with empty-object members#3915pullfrog[bot] wants to merge 1 commit into
z.union for discriminated unions with empty-object members#3915pullfrog[bot] wants to merge 1 commit into
Conversation
…ject members
When an OpenAPI oneOf/anyOf discriminator referenced an empty object schema (no properties), the Zod codegen emitted that schema as `z.record(...)` and then attempted to call `.extend({ discriminator: z.literal(value) })` on it, which does not type-check. Detect record-shaped refs in `tryBuildDiscriminatedUnion` and return null so the existing `z.union(...)` fallback path renders `z.object({...}).and(zEmpty)`-style members that compile.
Closes #3914
|
|
🦋 Changeset detectedLatest commit: 8fa9f3a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
@hey-api/codegen-core
@hey-api/json-schema-ref-parser
@hey-api/nuxt
@hey-api/openapi-ts
@hey-api/shared
@hey-api/spec-types
@hey-api/types
@hey-api/vite-plugin
commit: |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3915 +/- ##
==========================================
- Coverage 37.70% 37.69% -0.02%
==========================================
Files 582 582
Lines 20844 20851 +7
Branches 6063 6067 +4
==========================================
Hits 7860 7860
- Misses 10570 10573 +3
- Partials 2414 2418 +4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3914.
Problem
When an OpenAPI
oneOf/anyOfwith a discriminator referenced an empty object schema (type: objectwith no properties), the generated Zod schema failed to type-check:zEmptyis rendered asz.record(...)(because the empty object has no properties but the parser injectsadditionalProperties: { type: 'unknown' }), andz.record(...)has no.extend(...)method.Fix
tryBuildDiscriminatedUnioninpackages/openapi-ts/src/plugins/zod/shared/discriminated-union.tsnow resolves each$refmember and returnsnullwhen the resolved schema is record-shaped (type: 'object', noproperties,additionalPropertiesset). This delegates to the existingz.union(...)fallback path, which renders compileablez.object({ type: z.literal(...) }).and(zEmpty)members forv3/v4andz.intersection(...)formini.Tests
Added
discriminator-empty-object-memberscenario to all four zod test files (v3/v4×3.0.x/3.1.x) with newspecs/{3.0.x,3.1.x}/discriminator-empty-object-member.yamlinputs and 12 generated snapshots (3 dialects × 2 OAS versions × 2 zod packages).pnpm vitest run --project @test/openapi-ts-zod-v3 --project @test/openapi-ts-zod-v4— 210 passedpnpm ty @hey-api/openapi-ts— cleanpnpm lint— no new warningsClaude Opus| 𝕏