diff --git a/.changeset/eighty-rivers-enjoy.md b/.changeset/eighty-rivers-enjoy.md new file mode 100644 index 000000000..c7b208876 --- /dev/null +++ b/.changeset/eighty-rivers-enjoy.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": patch +--- + +fix: make descriminator field required when used with `oneOf` keyword diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/discriminator-mapped-many/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/discriminator-mapped-many/types.gen.ts index 1f6c5f2f2..e48cc7825 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/discriminator-mapped-many/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/discriminator-mapped-many/types.gen.ts @@ -1,9 +1,9 @@ // This file is auto-generated by @hey-api/openapi-ts export type Foo = ({ - foo?: 'one' | 'two'; + foo: 'one' | 'two'; } & Bar) | ({ - foo?: 'three'; + foo: 'three'; } & Baz); export type Bar = { diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/discriminator-one-of/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/discriminator-one-of/types.gen.ts index cdd9c641f..c739a4d95 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/discriminator-one-of/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/discriminator-one-of/types.gen.ts @@ -1,9 +1,9 @@ // This file is auto-generated by @hey-api/openapi-ts export type Foo = ({ - type?: 'Bar'; + type: 'Bar'; } & Bar) | ({ - type?: 'Baz'; + type: 'Baz'; } & Baz); export type Baz = Qux; @@ -18,9 +18,9 @@ export type Qux = { export type Quux = 'Bar' | 'Baz'; export type Quuz = ({ - type?: 'bar'; + type: 'bar'; } & Bar) | ({ - type?: 'baz'; + type: 'baz'; } & Baz); export type ClientOptions = { diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@hey-api/sdk/default/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@hey-api/sdk/default/types.gen.ts index 4f2e24b56..c2e9e8e23 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@hey-api/sdk/default/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@hey-api/sdk/default/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@hey-api/sdk/throwOnError/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@hey-api/sdk/throwOnError/types.gen.ts index 4f2e24b56..c2e9e8e23 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@hey-api/sdk/throwOnError/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@hey-api/sdk/throwOnError/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/angular-query-experimental/asClass/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/angular-query-experimental/asClass/types.gen.ts index 4f2e24b56..c2e9e8e23 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/angular-query-experimental/asClass/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/angular-query-experimental/asClass/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/angular-query-experimental/axios/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/angular-query-experimental/axios/types.gen.ts index a152624a2..170641a8a 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/angular-query-experimental/axios/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/angular-query-experimental/axios/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/angular-query-experimental/fetch/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/angular-query-experimental/fetch/types.gen.ts index 4f2e24b56..c2e9e8e23 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/angular-query-experimental/fetch/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/angular-query-experimental/fetch/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/react-query/asClass/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/react-query/asClass/types.gen.ts index 4f2e24b56..c2e9e8e23 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/react-query/asClass/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/react-query/asClass/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/react-query/axios/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/react-query/axios/types.gen.ts index a152624a2..170641a8a 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/react-query/axios/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/react-query/axios/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/react-query/fetch/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/react-query/fetch/types.gen.ts index 4f2e24b56..c2e9e8e23 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/react-query/fetch/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/react-query/fetch/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/solid-query/asClass/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/solid-query/asClass/types.gen.ts index 4f2e24b56..c2e9e8e23 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/solid-query/asClass/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/solid-query/asClass/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/solid-query/axios/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/solid-query/axios/types.gen.ts index a152624a2..170641a8a 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/solid-query/axios/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/solid-query/axios/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/solid-query/fetch/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/solid-query/fetch/types.gen.ts index 4f2e24b56..c2e9e8e23 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/solid-query/fetch/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/solid-query/fetch/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/svelte-query/asClass/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/svelte-query/asClass/types.gen.ts index 4f2e24b56..c2e9e8e23 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/svelte-query/asClass/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/svelte-query/asClass/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/svelte-query/axios/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/svelte-query/axios/types.gen.ts index a152624a2..170641a8a 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/svelte-query/axios/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/svelte-query/axios/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/svelte-query/fetch/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/svelte-query/fetch/types.gen.ts index 4f2e24b56..c2e9e8e23 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/svelte-query/fetch/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/svelte-query/fetch/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/vue-query/asClass/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/vue-query/asClass/types.gen.ts index 4f2e24b56..c2e9e8e23 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/vue-query/asClass/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/vue-query/asClass/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/vue-query/axios/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/vue-query/axios/types.gen.ts index a152624a2..170641a8a 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/vue-query/axios/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/vue-query/axios/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/vue-query/fetch/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/vue-query/fetch/types.gen.ts index 4f2e24b56..c2e9e8e23 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/vue-query/fetch/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/vue-query/fetch/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/fastify/default/types.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/fastify/default/types.gen.ts index 4f2e24b56..c2e9e8e23 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/fastify/default/types.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/fastify/default/types.gen.ts @@ -442,9 +442,9 @@ export type ModelSquare = { * This is a model with one property with a 'one of' relationship where the options are not $ref */ export type CompositionWithOneOfDiscriminator = ({ - kind?: 'circle'; + kind: 'circle'; } & ModelCircle) | ({ - kind?: 'square'; + kind: 'square'; } & ModelSquare); /** diff --git a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/zod/default/zod.gen.ts b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/zod/default/zod.gen.ts index c4e3b9123..ebd85ab94 100644 --- a/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/zod/default/zod.gen.ts +++ b/packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/zod/default/zod.gen.ts @@ -274,10 +274,10 @@ export const zModelSquare = z.object({ export const zCompositionWithOneOfDiscriminator = z.union([ z.object({ - kind: z.literal('circle').optional() + kind: z.literal('circle') }).and(zModelCircle), z.object({ - kind: z.literal('square').optional() + kind: z.literal('square') }).and(zModelSquare) ]); diff --git a/packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts b/packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts index 15b5b4bf9..f43caaf3e 100644 --- a/packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts +++ b/packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts @@ -710,6 +710,7 @@ const parseOneOf = ({ } : valueSchemas[0]!, }, + required: [schema.discriminator.propertyName], type: 'object', }; irCompositionSchema = { diff --git a/packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts b/packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts index 315fceb63..a5d2405c5 100644 --- a/packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts +++ b/packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts @@ -423,6 +423,7 @@ const parseAllOf = ({ } : valueSchemas[0]!, }, + required: [ref.discriminator.propertyName], type: 'object', }; if (ref.required?.includes(ref.discriminator.propertyName)) {