From 4e963baf2aab1593fd6deed62df6b5f6c95f94a1 Mon Sep 17 00:00:00 2001 From: Sergey Volkov Date: Wed, 9 Aug 2023 20:08:28 +0300 Subject: [PATCH 1/2] fix: problem with any discriminator base type --- .../base-schema-parsers/discriminator.js | 5 +- tests/spec/discriminator/expected.ts | 71 ++++++++++ tests/spec/discriminator/schema.json | 124 ++++++++++++++++++ tests/spec/discriminator/schema.ts | 71 ++++++++++ 4 files changed, 270 insertions(+), 1 deletion(-) diff --git a/src/schema-parser/base-schema-parsers/discriminator.js b/src/schema-parser/base-schema-parsers/discriminator.js index 2ae3ff17..478cd3e3 100644 --- a/src/schema-parser/base-schema-parsers/discriminator.js +++ b/src/schema-parser/base-schema-parsers/discriminator.js @@ -259,9 +259,12 @@ class DiscriminatorSchemaParser extends MonoSchemaParser { const { discriminator, ...noDiscriminatorSchema } = this.schema; const complexSchemaKeys = _.keys(this.schemaParser._complexSchemaParsers); const schema = _.omit(_.clone(noDiscriminatorSchema), complexSchemaKeys); + const schemaIsAny = + this.schemaParserFabric.getInlineParseContent(_.cloneDeep(schema)) === + this.config.Ts.Keyword.Any; const schemaIsEmpty = !_.keys(schema).length; - if (schemaIsEmpty) return null; + if (schemaIsEmpty || schemaIsAny) return null; const typeName = this.schemaUtils.resolveTypeName(this.typeName, { prefixes: this.config.extractingOptions.discriminatorAbstractPrefix, diff --git a/tests/spec/discriminator/expected.ts b/tests/spec/discriminator/expected.ts index 637ccd67..92ca7e37 100644 --- a/tests/spec/discriminator/expected.ts +++ b/tests/spec/discriminator/expected.ts @@ -113,6 +113,77 @@ export type InvalidDiscriminatorPropertyName = BaseInvalidDiscriminatorPropertyN | BaseInvalidDiscriminatorPropertyNameTypeMapping<"str", string> ); +/** kek pek */ +export type Variant = + | ({ + type: "update"; + } & VariantUpdate) + | ({ + type: "undo"; + } & VariantUndo) + | ({ + type: "rollback"; + } & VariantRollback) + | ({ + type: "scale"; + } & VariantScale) + | ({ + type: "resources"; + } & VariantResources) + | ({ + type: "firewall"; + } & VariantFirewall) + | ({ + type: "gateway"; + } & VariantGateway); + +/** Proposal to change firewall rules for deployment. */ +export interface VariantFirewall { + /** asdasdasdasdasdsad added to deployment. If not set, no rules are added. */ + rules_added?: string[]; + /** asdasdasdasdasdsad removed from deployment. If not set, no rules were removed. */ + rules_removed?: string[]; +} + +/** asdasdasdasdasd */ +export interface VariantScale { + /** + * asdasdasdasdasdsad + * @example 3 + */ + replicas: number; +} + +/** asdasdasdasdasd */ +export interface VariantResources { + resources: string; +} + +/** asdasdasdasdasd */ +export interface VariantGateway { + /** asdasdasdasdasdsad */ + port?: string; + /** asdasdasdasdasdsad */ + name?: string; + /** asdasdasdasdasdsad */ + domain?: string; +} + +/** Pasdasdasdasdasd. */ +export type VariantUpdate = object; + +/** asdasdasdasdasd */ +export interface VariantRollback { + /** + * asdasdasdasdasdsad + * @example 42 + */ + revision_id: number; +} + +/** asdasdasdasdasdn */ +export type VariantUndo = object; + interface BaseBlockDtoWithEnum { title: string; type: BlockDTOEnum; diff --git a/tests/spec/discriminator/schema.json b/tests/spec/discriminator/schema.json index d35c3c04..e586f71c 100644 --- a/tests/spec/discriminator/schema.json +++ b/tests/spec/discriminator/schema.json @@ -377,6 +377,130 @@ } } } + }, + "Variant": { + "description": "kek pek", + "discriminator": { + "propertyName": "type", + "mapping": { + "update": "#/components/schemas/VariantUpdate", + "undo": "#/components/schemas/VariantUndo", + "rollback": "#/components/schemas/VariantRollback", + "scale": "#/components/schemas/VariantScale", + "resources": "#/components/schemas/VariantResources", + "firewall": "#/components/schemas/VariantFirewall", + "gateway": "#/components/schemas/VariantGateway" + } + }, + "oneOf": [ + { + "$ref": "#/components/schemas/VariantUpdate" + }, + { + "$ref": "#/components/schemas/VariantUndo" + }, + { + "$ref": "#/components/schemas/VariantRollback" + }, + { + "$ref": "#/components/schemas/VariantScale" + }, + { + "$ref": "#/components/schemas/VariantResources" + }, + { + "$ref": "#/components/schemas/VariantFirewall" + }, + { + "$ref": "#/components/schemas/VariantGateway" + } + ] + }, + "VariantFirewall": { + "type": "object", + "description": "Proposal to change firewall rules for deployment.", + "properties": { + "rules_added": { + "type": "array", + "description": "asdasdasdasdasdsad added to deployment. If not set, no rules are added.\n", + "items": { + "type": "string" + } + }, + "rules_removed": { + "type": "array", + "description": "asdasdasdasdasdsad removed from deployment. If not set, no rules were removed.\n", + "items": { + "type": "string" + } + } + } + }, + "VariantScale": { + "type": "object", + "description": "asdasdasdasdasd", + "required": [ + "replicas" + ], + "properties": { + "replicas": { + "type": "integer", + "description": "asdasdasdasdasdsad", + "example": 3 + } + } + }, + "VariantResources": { + "type": "object", + "description": "asdasdasdasdasd", + "required": [ + "resources" + ], + "properties": { + "resources": { + "type": "string" + } + } + }, + "VariantGateway": { + "type": "object", + "description": "asdasdasdasdasd", + "properties": { + "port": { + "type": "string", + "description": "asdasdasdasdasdsad" + }, + "name": { + "type": "string", + "description": "asdasdasdasdasdsad" + }, + "domain": { + "type": "string", + "description": "asdasdasdasdasdsad" + } + } + }, + "VariantUpdate": { + "type": "object", + "description": "Pasdasdasdasdasd." + }, + "VariantRollback": { + "type": "object", + "description": "asdasdasdasdasd", + "required": [ + "revision_id" + ], + "properties": { + "revision_id": { + "type": "integer", + "description": "asdasdasdasdasdsad", + "example": 42 + } + } + }, + "VariantUndo": { + "type": "object", + "description": "asdasdasdasdasdn" } } } diff --git a/tests/spec/discriminator/schema.ts b/tests/spec/discriminator/schema.ts index 637ccd67..92ca7e37 100644 --- a/tests/spec/discriminator/schema.ts +++ b/tests/spec/discriminator/schema.ts @@ -113,6 +113,77 @@ export type InvalidDiscriminatorPropertyName = BaseInvalidDiscriminatorPropertyN | BaseInvalidDiscriminatorPropertyNameTypeMapping<"str", string> ); +/** kek pek */ +export type Variant = + | ({ + type: "update"; + } & VariantUpdate) + | ({ + type: "undo"; + } & VariantUndo) + | ({ + type: "rollback"; + } & VariantRollback) + | ({ + type: "scale"; + } & VariantScale) + | ({ + type: "resources"; + } & VariantResources) + | ({ + type: "firewall"; + } & VariantFirewall) + | ({ + type: "gateway"; + } & VariantGateway); + +/** Proposal to change firewall rules for deployment. */ +export interface VariantFirewall { + /** asdasdasdasdasdsad added to deployment. If not set, no rules are added. */ + rules_added?: string[]; + /** asdasdasdasdasdsad removed from deployment. If not set, no rules were removed. */ + rules_removed?: string[]; +} + +/** asdasdasdasdasd */ +export interface VariantScale { + /** + * asdasdasdasdasdsad + * @example 3 + */ + replicas: number; +} + +/** asdasdasdasdasd */ +export interface VariantResources { + resources: string; +} + +/** asdasdasdasdasd */ +export interface VariantGateway { + /** asdasdasdasdasdsad */ + port?: string; + /** asdasdasdasdasdsad */ + name?: string; + /** asdasdasdasdasdsad */ + domain?: string; +} + +/** Pasdasdasdasdasd. */ +export type VariantUpdate = object; + +/** asdasdasdasdasd */ +export interface VariantRollback { + /** + * asdasdasdasdasdsad + * @example 42 + */ + revision_id: number; +} + +/** asdasdasdasdasdn */ +export type VariantUndo = object; + interface BaseBlockDtoWithEnum { title: string; type: BlockDTOEnum; From 53be5b940b590ba975aec5fee66a90b66c92fb3f Mon Sep 17 00:00:00 2001 From: Sergey Volkov Date: Wed, 9 Aug 2023 20:11:52 +0300 Subject: [PATCH 2/2] bump: up version to 13.0.3 --- CHANGELOG.md | 4 ++++ package-lock.json | 2 +- package.json | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1da9a5b5..b2820ee4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # next release +## 13.0.3 + +fix: problem with type `any` as base type of discriminator schema (cases when schema without discriminator is empty) + ## 13.0.2 fix: problem with incorrect settings type suffix for internal discriminator mappings diff --git a/package-lock.json b/package-lock.json index 762b456d..fb03ef12 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "swagger-typescript-api", - "version": "13.0.2", + "version": "13.0.3", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/package.json b/package.json index f7764597..ab1bffd8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "swagger-typescript-api", - "version": "13.0.2", + "version": "13.0.3", "description": "Generate typescript/javascript api from swagger schema", "scripts": { "update-deps-to-latest": "npx --yes npm-check-updates && npm i",