Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-zod-discriminated-union-multi-value.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix(zod): generate z.discriminatedUnion when discriminator maps multiple values to the same schema
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) })
]);
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') })
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.literal(1) }),
zBar.extend({ code: z.literal(2) }),
zBaz.extend({ code: z.literal(3) })
]);
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') })
]);
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) })
]);
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') })
]);
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) })
]);
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') })
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.literal(1) }),
zBar.extend({ code: z.literal(2) }),
zBaz.extend({ code: z.literal(3) })
]);
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') })
]);
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) })
]);
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 packages/openapi-ts-tests/zod/v3/test/3.0.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ for (const zodVersion of zodVersions) {
}),
description: 'generates circular schemas',
},
{
config: createConfig({
input: 'discriminator-mapped-many.yaml',
output: 'discriminator-mapped-many',
}),
description:
'generates discriminated union when multiple mapping values point to same schema',
},
{
config: createConfig({
input: 'discriminator-mapped-many-number.yaml',
output: 'discriminator-mapped-many-number',
}),
description:
'generates discriminated union when multiple number mapping values point to same schema',
},
{
config: createConfig({
input: 'enum-null.json',
Expand Down
16 changes: 16 additions & 0 deletions packages/openapi-ts-tests/zod/v3/test/3.1.x.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ for (const zodVersion of zodVersions) {
}),
description: 'handles oneOf discriminator (falls back to z.union when needed)',
},
{
config: createConfig({
input: 'discriminator-mapped-many.yaml',
output: 'discriminator-mapped-many',
}),
description:
'generates discriminated union when multiple mapping values point to same schema',
},
Comment thread
CallumJHays marked this conversation as resolved.
{
config: createConfig({
input: 'discriminator-mapped-many-number.yaml',
output: 'discriminator-mapped-many-number',
}),
description:
'generates discriminated union when multiple number mapping values point to same schema',
},
{
config: createConfig({
input: 'enum-null.json',
Expand Down
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) })
]);
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') })
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.literal(1) }),
zBar.extend({ code: z.literal(2) }),
zBaz.extend({ code: z.literal(3) })
]);
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') })
]);
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) })
]);
Loading
Loading