-
-
Notifications
You must be signed in to change notification settings - Fork 366
fix(zod): generate z.discriminatedUnion for multi-value discriminator mappings #3867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
aa5a2e3
fix(zod): generate z.discriminatedUnion for multi-value discriminator…
CallumJHays cef8ae5
test(zod): add failing test for number-typed discriminator in mapped-…
CallumJHays 38101c4
fix(zod): use z.union of z.literal for non-string multi-value discrim…
CallumJHays File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@hey-api/openapi-ts': patch | ||
| --- | ||
|
|
||
| **plugin(zod)**: fix: generate z.discriminatedUnion when discriminator maps multiple values to the same schema |
16 changes: 16 additions & 0 deletions
16
...napi-ts-tests/zod/v3/__snapshots__/3.0.x/mini/discriminator-mapped-many-number/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import * as z from 'zod/v4-mini'; | ||
|
|
||
| export const zBar = z.object({ | ||
| code: z.optional(z.union([z.literal(1), z.literal(2)])) | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| code: z.optional(z.literal(3)) | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('code', [ | ||
| z.extend(zBar, { code: z.union([z.literal(1), z.literal(2)]) }), | ||
| z.extend(zBaz, { code: z.literal(3) }) | ||
| ]); |
21 changes: 21 additions & 0 deletions
21
...ges/openapi-ts-tests/zod/v3/__snapshots__/3.0.x/mini/discriminator-mapped-many/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import * as z from 'zod/v4-mini'; | ||
|
|
||
| export const zBar = z.object({ | ||
| foo: z.optional(z.enum(['one', 'two'])) | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| foo: z.optional(z.enum(['three'])) | ||
| }); | ||
|
|
||
| export const zSpæcial = z.object({ | ||
| foo: z.optional(z.enum(['four'])) | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('foo', [ | ||
| z.extend(zBar, { foo: z.enum(['one', 'two']) }), | ||
| z.extend(zBaz, { foo: z.literal('three') }), | ||
| z.extend(zSpæcial, { foo: z.literal('four') }) | ||
| ]); |
16 changes: 16 additions & 0 deletions
16
...penapi-ts-tests/zod/v3/__snapshots__/3.0.x/v3/discriminator-mapped-many-number/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import { z } from 'zod'; | ||
|
|
||
| export const zBar = z.object({ | ||
| code: z.union([z.literal(1), z.literal(2)]).optional() | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| code: z.literal(3).optional() | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('code', [ | ||
| zBar.extend({ code: z.union([z.literal(1), z.literal(2)]) }), | ||
| zBaz.extend({ code: z.literal(3) }) | ||
| ]); | ||
21 changes: 21 additions & 0 deletions
21
packages/openapi-ts-tests/zod/v3/__snapshots__/3.0.x/v3/discriminator-mapped-many/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import { z } from 'zod'; | ||
|
|
||
| export const zBar = z.object({ | ||
| foo: z.enum(['one', 'two']).optional() | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| foo: z.enum(['three']).optional() | ||
| }); | ||
|
|
||
| export const zSpæcial = z.object({ | ||
| foo: z.enum(['four']).optional() | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('foo', [ | ||
| zBar.extend({ foo: z.enum(['one', 'two']) }), | ||
| zBaz.extend({ foo: z.literal('three') }), | ||
| zSpæcial.extend({ foo: z.literal('four') }) | ||
| ]); |
16 changes: 16 additions & 0 deletions
16
...penapi-ts-tests/zod/v3/__snapshots__/3.0.x/v4/discriminator-mapped-many-number/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import * as z from 'zod/v4'; | ||
|
|
||
| export const zBar = z.object({ | ||
| code: z.union([z.literal(1), z.literal(2)]).optional() | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| code: z.literal(3).optional() | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('code', [ | ||
| zBar.extend({ code: z.union([z.literal(1), z.literal(2)]) }), | ||
| zBaz.extend({ code: z.literal(3) }) | ||
| ]); |
21 changes: 21 additions & 0 deletions
21
packages/openapi-ts-tests/zod/v3/__snapshots__/3.0.x/v4/discriminator-mapped-many/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import * as z from 'zod/v4'; | ||
|
|
||
| export const zBar = z.object({ | ||
| foo: z.enum(['one', 'two']).optional() | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| foo: z.enum(['three']).optional() | ||
| }); | ||
|
|
||
| export const zSpæcial = z.object({ | ||
| foo: z.enum(['four']).optional() | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('foo', [ | ||
| zBar.extend({ foo: z.enum(['one', 'two']) }), | ||
| zBaz.extend({ foo: z.literal('three') }), | ||
| zSpæcial.extend({ foo: z.literal('four') }) | ||
| ]); |
16 changes: 16 additions & 0 deletions
16
...napi-ts-tests/zod/v3/__snapshots__/3.1.x/mini/discriminator-mapped-many-number/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import * as z from 'zod/v4-mini'; | ||
|
|
||
| export const zBar = z.object({ | ||
| code: z.optional(z.union([z.literal(1), z.literal(2)])) | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| code: z.optional(z.literal(3)) | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('code', [ | ||
| z.extend(zBar, { code: z.union([z.literal(1), z.literal(2)]) }), | ||
| z.extend(zBaz, { code: z.literal(3) }) | ||
| ]); |
21 changes: 21 additions & 0 deletions
21
...ges/openapi-ts-tests/zod/v3/__snapshots__/3.1.x/mini/discriminator-mapped-many/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import * as z from 'zod/v4-mini'; | ||
|
|
||
| export const zBar = z.object({ | ||
| foo: z.optional(z.enum(['one', 'two'])) | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| foo: z.optional(z.enum(['three'])) | ||
| }); | ||
|
|
||
| export const zSpæcial = z.object({ | ||
| foo: z.optional(z.enum(['four'])) | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('foo', [ | ||
| z.extend(zBar, { foo: z.enum(['one', 'two']) }), | ||
| z.extend(zBaz, { foo: z.literal('three') }), | ||
| z.extend(zSpæcial, { foo: z.literal('four') }) | ||
| ]); |
16 changes: 16 additions & 0 deletions
16
...penapi-ts-tests/zod/v3/__snapshots__/3.1.x/v3/discriminator-mapped-many-number/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import { z } from 'zod'; | ||
|
|
||
| export const zBar = z.object({ | ||
| code: z.union([z.literal(1), z.literal(2)]).optional() | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| code: z.literal(3).optional() | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('code', [ | ||
| zBar.extend({ code: z.union([z.literal(1), z.literal(2)]) }), | ||
| zBaz.extend({ code: z.literal(3) }) | ||
| ]); |
21 changes: 21 additions & 0 deletions
21
packages/openapi-ts-tests/zod/v3/__snapshots__/3.1.x/v3/discriminator-mapped-many/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import { z } from 'zod'; | ||
|
|
||
| export const zBar = z.object({ | ||
| foo: z.enum(['one', 'two']).optional() | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| foo: z.enum(['three']).optional() | ||
| }); | ||
|
|
||
| export const zSpæcial = z.object({ | ||
| foo: z.enum(['four']).optional() | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('foo', [ | ||
| zBar.extend({ foo: z.enum(['one', 'two']) }), | ||
| zBaz.extend({ foo: z.literal('three') }), | ||
| zSpæcial.extend({ foo: z.literal('four') }) | ||
| ]); |
16 changes: 16 additions & 0 deletions
16
...penapi-ts-tests/zod/v3/__snapshots__/3.1.x/v4/discriminator-mapped-many-number/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import * as z from 'zod/v4'; | ||
|
|
||
| export const zBar = z.object({ | ||
| code: z.union([z.literal(1), z.literal(2)]).optional() | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| code: z.literal(3).optional() | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('code', [ | ||
| zBar.extend({ code: z.union([z.literal(1), z.literal(2)]) }), | ||
| zBaz.extend({ code: z.literal(3) }) | ||
| ]); |
21 changes: 21 additions & 0 deletions
21
packages/openapi-ts-tests/zod/v3/__snapshots__/3.1.x/v4/discriminator-mapped-many/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import * as z from 'zod/v4'; | ||
|
|
||
| export const zBar = z.object({ | ||
| foo: z.enum(['one', 'two']).optional() | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| foo: z.enum(['three']).optional() | ||
| }); | ||
|
|
||
| export const zSpæcial = z.object({ | ||
| foo: z.enum(['four']).optional() | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('foo', [ | ||
| zBar.extend({ foo: z.enum(['one', 'two']) }), | ||
| zBaz.extend({ foo: z.literal('three') }), | ||
| zSpæcial.extend({ foo: z.literal('four') }) | ||
| ]); |
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
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
16 changes: 16 additions & 0 deletions
16
...napi-ts-tests/zod/v4/__snapshots__/3.0.x/mini/discriminator-mapped-many-number/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import * as z from 'zod/mini'; | ||
|
|
||
| export const zBar = z.object({ | ||
| code: z.optional(z.union([z.literal(1), z.literal(2)])) | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| code: z.optional(z.literal(3)) | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('code', [ | ||
| z.extend(zBar, { code: z.union([z.literal(1), z.literal(2)]) }), | ||
| z.extend(zBaz, { code: z.literal(3) }) | ||
| ]); |
21 changes: 21 additions & 0 deletions
21
...ges/openapi-ts-tests/zod/v4/__snapshots__/3.0.x/mini/discriminator-mapped-many/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import * as z from 'zod/mini'; | ||
|
|
||
| export const zBar = z.object({ | ||
| foo: z.optional(z.enum(['one', 'two'])) | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| foo: z.optional(z.enum(['three'])) | ||
| }); | ||
|
|
||
| export const zSpæcial = z.object({ | ||
| foo: z.optional(z.enum(['four'])) | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('foo', [ | ||
| z.extend(zBar, { foo: z.enum(['one', 'two']) }), | ||
| z.extend(zBaz, { foo: z.literal('three') }), | ||
| z.extend(zSpæcial, { foo: z.literal('four') }) | ||
| ]); |
16 changes: 16 additions & 0 deletions
16
...penapi-ts-tests/zod/v4/__snapshots__/3.0.x/v3/discriminator-mapped-many-number/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import { z } from 'zod/v3'; | ||
|
|
||
| export const zBar = z.object({ | ||
| code: z.union([z.literal(1), z.literal(2)]).optional() | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| code: z.literal(3).optional() | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('code', [ | ||
| zBar.extend({ code: z.union([z.literal(1), z.literal(2)]) }), | ||
| zBaz.extend({ code: z.literal(3) }) | ||
| ]); |
21 changes: 21 additions & 0 deletions
21
packages/openapi-ts-tests/zod/v4/__snapshots__/3.0.x/v3/discriminator-mapped-many/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import { z } from 'zod/v3'; | ||
|
|
||
| export const zBar = z.object({ | ||
| foo: z.enum(['one', 'two']).optional() | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| foo: z.enum(['three']).optional() | ||
| }); | ||
|
|
||
| export const zSpæcial = z.object({ | ||
| foo: z.enum(['four']).optional() | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('foo', [ | ||
| zBar.extend({ foo: z.enum(['one', 'two']) }), | ||
| zBaz.extend({ foo: z.literal('three') }), | ||
| zSpæcial.extend({ foo: z.literal('four') }) | ||
| ]); |
16 changes: 16 additions & 0 deletions
16
...penapi-ts-tests/zod/v4/__snapshots__/3.0.x/v4/discriminator-mapped-many-number/zod.gen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import * as z from 'zod'; | ||
|
|
||
| export const zBar = z.object({ | ||
| code: z.union([z.literal(1), z.literal(2)]).optional() | ||
| }); | ||
|
|
||
| export const zBaz = z.object({ | ||
| code: z.literal(3).optional() | ||
| }); | ||
|
|
||
| export const zFoo = z.discriminatedUnion('code', [ | ||
| zBar.extend({ code: z.union([z.literal(1), z.literal(2)]) }), | ||
| zBaz.extend({ code: z.literal(3) }) | ||
| ]); |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This snapshot locks in code that throws at module load under
zod@3.x. Reproduction:z.discriminatedUnion('code', [zBar.extend({ code: z.union([z.literal(1), z.literal(2)]) }), zBaz.extend({ code: z.literal(3) })])→Error: A discriminator value for key \code` could not be extracted from all schema options. Fix the v3 emitter so this becomes one discriminated-union entry per literal value (e.g. one branch forcode: z.literal(1), one forcode: z.literal(2), one forcode: z.literal(3)`), then regenerate.