From 6c71884c9f34ebbcfa59ff5e9129088c55862bda Mon Sep 17 00:00:00 2001 From: Sergey Volkov Date: Tue, 25 Jul 2023 19:39:34 +0300 Subject: [PATCH] bump: 13.0.2 version; fix: problem with incorrect settings type suffix for internal discriminator mappings --- CHANGELOG.md | 6 +++ package-lock.json | 4 +- package.json | 2 +- .../base-schema-parsers/discriminator.js | 52 +++++++++---------- src/schema-parser/schema-parser-fabric.js | 1 + src/type-name-formatter.js | 8 +-- 6 files changed, 37 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83cde646..1da9a5b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # next release +## 13.0.2 + +fix: problem with incorrect settings type suffix for internal discriminator mappings + +## 13.0.1 + feat: `const` keyword OpenAPI 3.0 draft fix: problem with using `anyOf` feat: `--extract-responses` (nodejs: `extractResponses`) option to extract all schemas from `/components/responses` diff --git a/package-lock.json b/package-lock.json index 813b6046..762b456d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "swagger-typescript-api", - "version": "13.0.0", + "version": "13.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "swagger-typescript-api", - "version": "13.0.0", + "version": "13.0.2", "license": "MIT", "dependencies": { "@types/swagger-schema-official": "2.0.22", diff --git a/package.json b/package.json index 27ecda64..f7764597 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "swagger-typescript-api", - "version": "13.0.0", + "version": "13.0.2", "description": "Generate typescript/javascript api from swagger schema", "scripts": { "update-deps-to-latest": "npx --yes npm-check-updates && npm i", diff --git a/src/schema-parser/base-schema-parsers/discriminator.js b/src/schema-parser/base-schema-parsers/discriminator.js index e644983d..2ae3ff17 100644 --- a/src/schema-parser/base-schema-parsers/discriminator.js +++ b/src/schema-parser/base-schema-parsers/discriminator.js @@ -74,36 +74,36 @@ class DiscriminatorSchemaParser extends MonoSchemaParser { }); if (ableToCreateMappingType) { - const mappingTypeNameRef = this.schemaComponentsMap.createRef([ - 'components', - 'schemas', - this.schemaUtils.resolveTypeName( - `${abstractSchemaStruct.typeName} ${discriminator.propertyName}`, - { - suffixes: this.config.extractingOptions.discriminatorMappingSuffix, - resolver: - this.config.extractingOptions.discriminatorMappingNameResolver, - }, + const rawTypeName = `${abstractSchemaStruct.typeName}_${discriminator.propertyName}`; + const generatedTypeName = this.schemaUtils.resolveTypeName(rawTypeName, { + suffixes: this.config.extractingOptions.discriminatorMappingSuffix, + resolver: + this.config.extractingOptions.discriminatorMappingNameResolver, + }); + + const content = ts.IntersectionType([ + ts.ObjectWrapper( + ts.TypeField({ + key: ts.StringValue(discriminator.propertyName), + value: 'Key', + }), ), + 'Type', ]); - const mappingTypeNameComponent = - this.schemaComponentsMap.createComponent(mappingTypeNameRef); - const mappingTypeNameSchema = this.schemaParserFabric.createSchema({ - linkedComponent: mappingTypeNameComponent, - content: ts.IntersectionType([ - ts.ObjectWrapper( - ts.TypeField({ - key: ts.StringValue(discriminator.propertyName), - value: 'Key', - }), - ), - 'Type', - ]), - genericArgs: [{ name: 'Key' }, { name: 'Type' }], - internal: true, + + const component = this.schemaParserFabric.createParsedComponent({ + typeName: generatedTypeName, + schema: { + type: 'object', + properties: {}, + genericArgs: [{ name: 'Key' }, { name: 'Type' }], + internal: true, + }, }); - mappingTypeName = mappingTypeNameSchema.typeData.name; + component.typeData.content = content; + + mappingTypeName = this.typeNameFormatter.format(component.typeName); } /** returns (GenericType<"mapping_key", MappingType>) or ({ discriminatorProperty: "mapping_key" } & MappingType) */ diff --git a/src/schema-parser/schema-parser-fabric.js b/src/schema-parser/schema-parser-fabric.js index 0827e4f0..fbe93a45 100644 --- a/src/schema-parser/schema-parser-fabric.js +++ b/src/schema-parser/schema-parser-fabric.js @@ -79,6 +79,7 @@ class SchemaParserFabric { schemaCopy, ); const parsed = this.parseSchema(schemaCopy, null, schemaPath); + parsed.name = typeName; customComponent.typeData = parsed; diff --git a/src/type-name-formatter.js b/src/type-name-formatter.js index c3d23525..49bc1aee 100644 --- a/src/type-name-formatter.js +++ b/src/type-name-formatter.js @@ -41,13 +41,7 @@ class TypeNameFormatter { ? this.config.enumKeySuffix : this.config.typeSuffix; - const existedFormattedEntry = Array.from( - this.formattedModelNamesMap.entries(), - ).find((entry) => entry[1] === name); - - const hashKey = existedFormattedEntry - ? existedFormattedEntry[0] - : `${typePrefix}_${name}_${typeSuffix}`; + const hashKey = `${typePrefix}_${name}_${typeSuffix}`; if (typeof name !== 'string') { this.logger.warn('wrong name of the model name', name);