Skip to content

Commit d94c9ae

Browse files
authored
Merge pull request #2006 from Liooo/fix/oneof-discriminator-required
fix: make oneOf discriminator props required
2 parents 5deefc4 + aa10522 commit d94c9ae

File tree

24 files changed

+51
-44
lines changed

24 files changed

+51
-44
lines changed

.changeset/eighty-rivers-enjoy.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/openapi-ts": patch
3+
---
4+
5+
fix: make descriminator field required when used with `oneOf` keyword

packages/openapi-ts-tests/test/__snapshots__/3.0.x/discriminator-mapped-many/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

33
export type Foo = ({
4-
foo?: 'one' | 'two';
4+
foo: 'one' | 'two';
55
} & Bar) | ({
6-
foo?: 'three';
6+
foo: 'three';
77
} & Baz);
88

99
export type Bar = {

packages/openapi-ts-tests/test/__snapshots__/3.0.x/discriminator-one-of/types.gen.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

33
export type Foo = ({
4-
type?: 'Bar';
4+
type: 'Bar';
55
} & Bar) | ({
6-
type?: 'Baz';
6+
type: 'Baz';
77
} & Baz);
88

99
export type Baz = Qux;
@@ -18,9 +18,9 @@ export type Qux = {
1818
export type Quux = 'Bar' | 'Baz';
1919

2020
export type Quuz = ({
21-
type?: 'bar';
21+
type: 'bar';
2222
} & Bar) | ({
23-
type?: 'baz';
23+
type: 'baz';
2424
} & Baz);
2525

2626
export type ClientOptions = {

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@hey-api/sdk/default/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@hey-api/sdk/throwOnError/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/angular-query-experimental/asClass/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/angular-query-experimental/axios/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/angular-query-experimental/fetch/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/react-query/asClass/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/react-query/axios/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/react-query/fetch/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/solid-query/asClass/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/solid-query/axios/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/solid-query/fetch/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/svelte-query/asClass/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/svelte-query/axios/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/svelte-query/fetch/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/vue-query/asClass/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/vue-query/axios/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@tanstack/vue-query/fetch/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/fastify/default/types.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ export type ModelSquare = {
442442
* This is a model with one property with a 'one of' relationship where the options are not $ref
443443
*/
444444
export type CompositionWithOneOfDiscriminator = ({
445-
kind?: 'circle';
445+
kind: 'circle';
446446
} & ModelCircle) | ({
447-
kind?: 'square';
447+
kind: 'square';
448448
} & ModelSquare);
449449

450450
/**

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/zod/default/zod.gen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,10 @@ export const zModelSquare = z.object({
274274

275275
export const zCompositionWithOneOfDiscriminator = z.union([
276276
z.object({
277-
kind: z.literal('circle').optional()
277+
kind: z.literal('circle')
278278
}).and(zModelCircle),
279279
z.object({
280-
kind: z.literal('square').optional()
280+
kind: z.literal('square')
281281
}).and(zModelSquare)
282282
]);
283283

packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts

+1
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ const parseOneOf = ({
710710
}
711711
: valueSchemas[0]!,
712712
},
713+
required: [schema.discriminator.propertyName],
713714
type: 'object',
714715
};
715716
irCompositionSchema = {

packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts

+1
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ const parseAllOf = ({
423423
}
424424
: valueSchemas[0]!,
425425
},
426+
required: [ref.discriminator.propertyName],
426427
type: 'object',
427428
};
428429
if (ref.required?.includes(ref.discriminator.propertyName)) {

0 commit comments

Comments
 (0)