From 4e440b4af6b2de03886b624662de70cd0304ee7f Mon Sep 17 00:00:00 2001 From: Jeongho Nam Date: Fri, 5 Jul 2024 17:41:55 +0900 Subject: [PATCH] Add IJsonSchema.IOneOf.discriminator --- src/OpenApi.ts | 30 +++++++++++++++++++++++++++-- src/OpenApiV3.ts | 7 +++++++ src/OpenApiV3_1.ts | 7 +++++++ src/internal/SwaggerV2Downgrader.ts | 1 + 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/OpenApi.ts b/src/OpenApi.ts index e90be5f..ac7a25e 100644 --- a/src/OpenApi.ts +++ b/src/OpenApi.ts @@ -396,7 +396,7 @@ export namespace OpenApi { * Path item. * * `OpenApi.IPath` represents a path item of emended OpenAPI v3.1, - * collecting multiple method operations in a signgle path. + * collecting multiple method operations in a single path. */ export interface IPath< Schema extends IJsonSchema = IJsonSchema, @@ -787,7 +787,7 @@ export namespace OpenApi { } /** - * Array type. + * Array type info. */ export interface IArray extends __ISignificant<"array"> { @@ -986,6 +986,32 @@ export namespace OpenApi { * List of the union types. */ oneOf: Exclude[]; + + /** + * Discriminator info of the union type. + */ + discriminator?: IOneOf.IDiscriminator; + } + export namespace IOneOf { + /** + * Discriminator info of the union type. + */ + export interface IDiscriminator { + /** + * Property name for the discriminator. + */ + propertyName: string; + + /** + * Mapping of the discriminator value to the schema name. + * + * This property is valid only for {@link IReference} typed + * {@link IOneOf.oneof} elements. Therefore, `key` of `mapping` is + * the discriminator value, and `value` of `mapping` is the + * schema name like `#/components/schemas/SomeObject`. + */ + mapping?: Record; + } } /** diff --git a/src/OpenApiV3.ts b/src/OpenApiV3.ts index d4a540a..1a84317 100644 --- a/src/OpenApiV3.ts +++ b/src/OpenApiV3.ts @@ -251,6 +251,13 @@ export namespace OpenApiV3 { } export interface IOneOf extends __IAttribute { oneOf: IJsonSchema[]; + discriminator?: IOneOf.IDiscriminator; + } + export namespace IOneOf { + export interface IDiscriminator { + propertyName: string; + mapping?: Record; + } } export interface __ISignificant extends __IAttribute { diff --git a/src/OpenApiV3_1.ts b/src/OpenApiV3_1.ts index 070cd38..8b4f2c2 100644 --- a/src/OpenApiV3_1.ts +++ b/src/OpenApiV3_1.ts @@ -262,6 +262,13 @@ export namespace OpenApiV3_1 { } export interface IOneOf extends __IAttribute { oneOf: IJsonSchema[]; + discriminator?: IOneOf.IDiscriminator; + } + export namespace IOneOf { + export interface IDiscriminator { + propertyName: string; + mapping?: Record; + } } export interface IArray extends __ISignificant<"array"> { diff --git a/src/internal/SwaggerV2Downgrader.ts b/src/internal/SwaggerV2Downgrader.ts index ce73eaf..cb7f389 100644 --- a/src/internal/SwaggerV2Downgrader.ts +++ b/src/internal/SwaggerV2Downgrader.ts @@ -291,6 +291,7 @@ export namespace SwaggerV2Downgrader { ? { ...union[0] } : { "x-oneOf": union.map((u) => ({ ...u, nullable: undefined })) }), ...attribute, + ...(union.length > 1 ? { discriminator: undefined } : {}), }; };