Skip to content

fix: make oneOf discriminator props required #2006

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

Merged
merged 4 commits into from
May 4, 2025
Merged
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/eighty-rivers-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: make descriminator field required when used with `oneOf` keyword
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]);

Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@
}
: valueSchemas[0]!,
},
required: [schema.discriminator.propertyName],

Check warning on line 713 in packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L713 was not covered by tests
type: 'object',
};
irCompositionSchema = {
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@
}
: valueSchemas[0]!,
},
required: [ref.discriminator.propertyName],

Check warning on line 426 in packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L426 was not covered by tests
type: 'object',
};
if (ref.required?.includes(ref.discriminator.propertyName)) {
Expand Down