From 7d81ef21f07e6a1f66378aaa966075077a50be91 Mon Sep 17 00:00:00 2001 From: Theo Mathieu Date: Tue, 30 May 2023 08:54:15 +0200 Subject: [PATCH 1/7] fix: pascal case --- .dockerignore | 1 + Dockerfile | 5 ++++- src/openApi/v2/parser/getModel.ts | 19 ++++++++++--------- src/openApi/v2/parser/getModelProperties.ts | 8 +++++--- src/openApi/v2/parser/getModels.ts | 8 +++++++- src/utils/toPascalCase.ts | 12 ++++++++++++ 6 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 .dockerignore create mode 100644 src/utils/toPascalCase.ts diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..3c3629e64 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +node_modules diff --git a/Dockerfile b/Dockerfile index 32f7eb32c..d087c578b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,10 @@ FROM node:alpine WORKDIR /usr/src/openapi + +COPY ["package.json", "package-lock.json", "./"] +RUN ["npm", "install"] + COPY . /usr/src/openapi -RUN npm install RUN npm run release ENTRYPOINT [ "node", "/usr/src/openapi/bin/index.js" ] CMD "--help" diff --git a/src/openApi/v2/parser/getModel.ts b/src/openApi/v2/parser/getModel.ts index 22f3528aa..3306aac4d 100644 --- a/src/openApi/v2/parser/getModel.ts +++ b/src/openApi/v2/parser/getModel.ts @@ -1,5 +1,6 @@ import type { Model } from '../../../client/interfaces/Model'; import { getPattern } from '../../../utils/getPattern'; +import { toPascalCase } from '../../../utils/toPascalCase'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import { extendEnum } from './extendEnum'; @@ -49,10 +50,10 @@ export const getModel = ( if (definition.$ref) { const definitionRef = getType(definition.$ref); model.export = 'reference'; - model.type = definitionRef.type; - model.base = definitionRef.base; + model.type = toPascalCase(definitionRef.type); + model.base = toPascalCase(definitionRef.base); model.template = definitionRef.template; - model.imports.push(...definitionRef.imports); + model.imports.push(...definitionRef.imports.map(toPascalCase)); return model; } @@ -72,10 +73,10 @@ export const getModel = ( if (definition.items.$ref) { const arrayItems = getType(definition.items.$ref); model.export = 'array'; - model.type = arrayItems.type; - model.base = arrayItems.base; + model.type = toPascalCase(arrayItems.type); + model.base = toPascalCase(arrayItems.base); model.template = arrayItems.template; - model.imports.push(...arrayItems.imports); + model.imports.push(...arrayItems.imports.map(toPascalCase)); return model; } else { const arrayItems = getModel(openApi, definition.items); @@ -93,10 +94,10 @@ export const getModel = ( if (definition.additionalProperties.$ref) { const additionalProperties = getType(definition.additionalProperties.$ref); model.export = 'dictionary'; - model.type = additionalProperties.type; - model.base = additionalProperties.base; + model.type = toPascalCase(additionalProperties.type); + model.base = toPascalCase(additionalProperties.base); model.template = additionalProperties.template; - model.imports.push(...additionalProperties.imports); + model.imports.push(...additionalProperties.imports.map(toPascalCase)); return model; } else { const additionalProperties = getModel(openApi, definition.additionalProperties); diff --git a/src/openApi/v2/parser/getModelProperties.ts b/src/openApi/v2/parser/getModelProperties.ts index 7560be765..0ee5d98ab 100644 --- a/src/openApi/v2/parser/getModelProperties.ts +++ b/src/openApi/v2/parser/getModelProperties.ts @@ -1,5 +1,6 @@ import type { Model } from '../../../client/interfaces/Model'; import { getPattern } from '../../../utils/getPattern'; +import { toPascalCase } from '../../../utils/toPascalCase'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import { escapeName } from './escapeName'; @@ -17,11 +18,12 @@ export const getModelProperties = (openApi: OpenApi, definition: OpenApiSchema, const propertyRequired = !!definition.required?.includes(propertyName); if (property.$ref) { const model = getType(property.$ref); + console.log(model); models.push({ name: escapeName(propertyName), export: 'reference', - type: model.type, - base: model.base, + type: toPascalCase(model.type), + base: toPascalCase(model.base), template: model.template, link: null, description: property.description || null, @@ -43,7 +45,7 @@ export const getModelProperties = (openApi: OpenApi, definition: OpenApiSchema, maxProperties: property.maxProperties, minProperties: property.minProperties, pattern: getPattern(property.pattern), - imports: model.imports, + imports: model.imports.map(toPascalCase), enum: [], enums: [], properties: [], diff --git a/src/openApi/v2/parser/getModels.ts b/src/openApi/v2/parser/getModels.ts index 4ea4728cc..f165fcf9d 100644 --- a/src/openApi/v2/parser/getModels.ts +++ b/src/openApi/v2/parser/getModels.ts @@ -1,5 +1,6 @@ import type { Model } from '../../../client/interfaces/Model'; import { reservedWords } from '../../../utils/reservedWords'; +import { toPascalCase } from '../../../utils/toPascalCase'; import type { OpenApi } from '../interfaces/OpenApi'; import { getModel } from './getModel'; import { getType } from './getType'; @@ -10,7 +11,12 @@ export const getModels = (openApi: OpenApi): Model[] => { if (openApi.definitions.hasOwnProperty(definitionName)) { const definition = openApi.definitions[definitionName]; const definitionType = getType(definitionName); - const model = getModel(openApi, definition, true, definitionType.base.replace(reservedWords, '_$1')); + const model = getModel( + openApi, + definition, + true, + toPascalCase(definitionType.base.replace(reservedWords, '_$1')) + ); models.push(model); } } diff --git a/src/utils/toPascalCase.ts b/src/utils/toPascalCase.ts new file mode 100644 index 000000000..1e1aab4db --- /dev/null +++ b/src/utils/toPascalCase.ts @@ -0,0 +1,12 @@ +const _capitalize = (string: string): string => { + return string.slice(0, 1).toUpperCase() + string.slice(1, string.length); +}; + +export const toPascalCase = (str: string): string => { + return str + .split('_') + .map(str => _capitalize(str.split('/').map(_capitalize).join('/'))) + .join('') + .replace(/(?:^\w|[A-Z]|\b\w)/g, word => word.toUpperCase()) + .replace(/\s+/g, ''); +}; From 62f6c5be39352b872bb2b36b50bdb796a8bac521 Mon Sep 17 00:00:00 2001 From: Theo Mathieu Date: Tue, 30 May 2023 08:55:34 +0200 Subject: [PATCH 2/7] feat: wip --- src/openApi/v2/parser/getModelProperties.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/openApi/v2/parser/getModelProperties.ts b/src/openApi/v2/parser/getModelProperties.ts index 0ee5d98ab..c9ac7b2cb 100644 --- a/src/openApi/v2/parser/getModelProperties.ts +++ b/src/openApi/v2/parser/getModelProperties.ts @@ -18,7 +18,6 @@ export const getModelProperties = (openApi: OpenApi, definition: OpenApiSchema, const propertyRequired = !!definition.required?.includes(propertyName); if (property.$ref) { const model = getType(property.$ref); - console.log(model); models.push({ name: escapeName(propertyName), export: 'reference', From b293edffbc20b6a4282cfabcefb4e629532c14b1 Mon Sep 17 00:00:00 2001 From: Theo Mathieu Date: Tue, 30 May 2023 09:19:53 +0200 Subject: [PATCH 3/7] fix: pascal case --- src/openApi/v2/parser/getModel.ts | 19 +++++++++---------- src/openApi/v2/parser/getModels.ts | 8 +------- src/openApi/v2/parser/getType.ts | 3 ++- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/openApi/v2/parser/getModel.ts b/src/openApi/v2/parser/getModel.ts index 3306aac4d..22f3528aa 100644 --- a/src/openApi/v2/parser/getModel.ts +++ b/src/openApi/v2/parser/getModel.ts @@ -1,6 +1,5 @@ import type { Model } from '../../../client/interfaces/Model'; import { getPattern } from '../../../utils/getPattern'; -import { toPascalCase } from '../../../utils/toPascalCase'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import { extendEnum } from './extendEnum'; @@ -50,10 +49,10 @@ export const getModel = ( if (definition.$ref) { const definitionRef = getType(definition.$ref); model.export = 'reference'; - model.type = toPascalCase(definitionRef.type); - model.base = toPascalCase(definitionRef.base); + model.type = definitionRef.type; + model.base = definitionRef.base; model.template = definitionRef.template; - model.imports.push(...definitionRef.imports.map(toPascalCase)); + model.imports.push(...definitionRef.imports); return model; } @@ -73,10 +72,10 @@ export const getModel = ( if (definition.items.$ref) { const arrayItems = getType(definition.items.$ref); model.export = 'array'; - model.type = toPascalCase(arrayItems.type); - model.base = toPascalCase(arrayItems.base); + model.type = arrayItems.type; + model.base = arrayItems.base; model.template = arrayItems.template; - model.imports.push(...arrayItems.imports.map(toPascalCase)); + model.imports.push(...arrayItems.imports); return model; } else { const arrayItems = getModel(openApi, definition.items); @@ -94,10 +93,10 @@ export const getModel = ( if (definition.additionalProperties.$ref) { const additionalProperties = getType(definition.additionalProperties.$ref); model.export = 'dictionary'; - model.type = toPascalCase(additionalProperties.type); - model.base = toPascalCase(additionalProperties.base); + model.type = additionalProperties.type; + model.base = additionalProperties.base; model.template = additionalProperties.template; - model.imports.push(...additionalProperties.imports.map(toPascalCase)); + model.imports.push(...additionalProperties.imports); return model; } else { const additionalProperties = getModel(openApi, definition.additionalProperties); diff --git a/src/openApi/v2/parser/getModels.ts b/src/openApi/v2/parser/getModels.ts index f165fcf9d..4ea4728cc 100644 --- a/src/openApi/v2/parser/getModels.ts +++ b/src/openApi/v2/parser/getModels.ts @@ -1,6 +1,5 @@ import type { Model } from '../../../client/interfaces/Model'; import { reservedWords } from '../../../utils/reservedWords'; -import { toPascalCase } from '../../../utils/toPascalCase'; import type { OpenApi } from '../interfaces/OpenApi'; import { getModel } from './getModel'; import { getType } from './getType'; @@ -11,12 +10,7 @@ export const getModels = (openApi: OpenApi): Model[] => { if (openApi.definitions.hasOwnProperty(definitionName)) { const definition = openApi.definitions[definitionName]; const definitionType = getType(definitionName); - const model = getModel( - openApi, - definition, - true, - toPascalCase(definitionType.base.replace(reservedWords, '_$1')) - ); + const model = getModel(openApi, definition, true, definitionType.base.replace(reservedWords, '_$1')); models.push(model); } } diff --git a/src/openApi/v2/parser/getType.ts b/src/openApi/v2/parser/getType.ts index 6caa1e015..efd485470 100644 --- a/src/openApi/v2/parser/getType.ts +++ b/src/openApi/v2/parser/getType.ts @@ -1,4 +1,5 @@ import type { Type } from '../../../client/interfaces/Type'; +import { toPascalCase } from '../../../utils/toPascalCase'; import { getMappedType } from './getMappedType'; import { stripNamespace } from './stripNamespace'; @@ -56,7 +57,7 @@ export const getType = (type: string = 'any', format?: string): Type => { } if (typeWithoutNamespace) { - const type = encode(typeWithoutNamespace); + const type = toPascalCase(encode(typeWithoutNamespace)); result.type = type; result.base = type; result.imports.push(type); From 0249ce44b88064018b3767fe96051ca5d640db68 Mon Sep 17 00:00:00 2001 From: Theo Mathieu Date: Tue, 30 May 2023 09:31:45 +0200 Subject: [PATCH 4/7] fix: pascal case --- src/openApi/v2/parser/getModelProperties.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/openApi/v2/parser/getModelProperties.ts b/src/openApi/v2/parser/getModelProperties.ts index c9ac7b2cb..7560be765 100644 --- a/src/openApi/v2/parser/getModelProperties.ts +++ b/src/openApi/v2/parser/getModelProperties.ts @@ -1,6 +1,5 @@ import type { Model } from '../../../client/interfaces/Model'; import { getPattern } from '../../../utils/getPattern'; -import { toPascalCase } from '../../../utils/toPascalCase'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import { escapeName } from './escapeName'; @@ -21,8 +20,8 @@ export const getModelProperties = (openApi: OpenApi, definition: OpenApiSchema, models.push({ name: escapeName(propertyName), export: 'reference', - type: toPascalCase(model.type), - base: toPascalCase(model.base), + type: model.type, + base: model.base, template: model.template, link: null, description: property.description || null, @@ -44,7 +43,7 @@ export const getModelProperties = (openApi: OpenApi, definition: OpenApiSchema, maxProperties: property.maxProperties, minProperties: property.minProperties, pattern: getPattern(property.pattern), - imports: model.imports.map(toPascalCase), + imports: model.imports, enum: [], enums: [], properties: [], From 685029f555f890981e41330f7ea7fac315c253df Mon Sep 17 00:00:00 2001 From: Theo Mathieu Date: Tue, 30 May 2023 09:34:15 +0200 Subject: [PATCH 5/7] fix: pascal case --- src/openApi/v2/parser/getType.spec.ts | 18 ++++----- test/__snapshots__/index.spec.ts.snap | 56 +++++++++++++-------------- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/openApi/v2/parser/getType.spec.ts b/src/openApi/v2/parser/getType.spec.ts index 575d2d46c..686bf1df1 100644 --- a/src/openApi/v2/parser/getType.spec.ts +++ b/src/openApi/v2/parser/getType.spec.ts @@ -51,25 +51,25 @@ describe('getType', () => { it('should support dot', () => { const type = getType('#/definitions/model.000'); - expect(type.type).toEqual('model_000'); - expect(type.base).toEqual('model_000'); + expect(type.type).toEqual('Model000'); + expect(type.base).toEqual('Model000'); expect(type.template).toEqual(null); - expect(type.imports).toEqual(['model_000']); + expect(type.imports).toEqual(['Model000']); }); it('should support dashes', () => { const type = getType('#/definitions/some_special-schema'); - expect(type.type).toEqual('some_special_schema'); - expect(type.base).toEqual('some_special_schema'); + expect(type.type).toEqual('SomeSpecialSchema'); + expect(type.base).toEqual('SomeSpecialSchema'); expect(type.template).toEqual(null); - expect(type.imports).toEqual(['some_special_schema']); + expect(type.imports).toEqual(['SomeSpecialSchema']); }); it('should support dollar sign', () => { const type = getType('#/definitions/$some+special+schema'); - expect(type.type).toEqual('$some_special_schema'); - expect(type.base).toEqual('$some_special_schema'); + expect(type.type).toEqual('$SomeSpecialSchema'); + expect(type.base).toEqual('$SomeSpecialSchema'); expect(type.template).toEqual(null); - expect(type.imports).toEqual(['$some_special_schema']); + expect(type.imports).toEqual(['$SomeSpecialSchema']); }); }); diff --git a/test/__snapshots__/index.spec.ts.snap b/test/__snapshots__/index.spec.ts.snap index bb00203da..b0f370ecf 100644 --- a/test/__snapshots__/index.spec.ts.snap +++ b/test/__snapshots__/index.spec.ts.snap @@ -563,7 +563,6 @@ export { CancelablePromise, CancelError } from './core/CancelablePromise'; export { OpenAPI } from './core/OpenAPI'; export type { OpenAPIConfig } from './core/OpenAPI'; -export type { _default } from './models/_default'; export type { ArrayWithArray } from './models/ArrayWithArray'; export type { ArrayWithBooleans } from './models/ArrayWithBooleans'; export type { ArrayWithNumbers } from './models/ArrayWithNumbers'; @@ -577,6 +576,7 @@ export type { CommentWithQuotes } from './models/CommentWithQuotes'; export type { CommentWithReservedCharacters } from './models/CommentWithReservedCharacters'; export type { CommentWithSlashes } from './models/CommentWithSlashes'; export type { Date } from './models/Date'; +export type { Default } from './models/Default'; export type { DictionaryWithArray } from './models/DictionaryWithArray'; export type { DictionaryWithDictionary } from './models/DictionaryWithDictionary'; export type { DictionaryWithProperties } from './models/DictionaryWithProperties'; @@ -612,7 +612,6 @@ export type { SimpleReference } from './models/SimpleReference'; export type { SimpleString } from './models/SimpleString'; export type { SimpleStringWithPattern } from './models/SimpleStringWithPattern'; -export { $_default } from './schemas/$_default'; export { $ArrayWithArray } from './schemas/$ArrayWithArray'; export { $ArrayWithBooleans } from './schemas/$ArrayWithBooleans'; export { $ArrayWithNumbers } from './schemas/$ArrayWithNumbers'; @@ -626,6 +625,7 @@ export { $CommentWithQuotes } from './schemas/$CommentWithQuotes'; export { $CommentWithReservedCharacters } from './schemas/$CommentWithReservedCharacters'; export { $CommentWithSlashes } from './schemas/$CommentWithSlashes'; export { $Date } from './schemas/$Date'; +export { $Default } from './schemas/$Default'; export { $DictionaryWithArray } from './schemas/$DictionaryWithArray'; export { $DictionaryWithDictionary } from './schemas/$DictionaryWithDictionary'; export { $DictionaryWithProperties } from './schemas/$DictionaryWithProperties'; @@ -680,18 +680,6 @@ export { TypesService } from './services/TypesService'; " `; -exports[`v2 should generate: test/generated/v2/models/_default.ts 1`] = ` -"/* istanbul ignore file */ -/* tslint:disable */ -/* eslint-disable */ - -export type _default = { - name?: string; -}; - -" -`; - exports[`v2 should generate: test/generated/v2/models/ArrayWithArray.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ @@ -858,6 +846,18 @@ export type Date = string; " `; +exports[`v2 should generate: test/generated/v2/models/Default.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ + +export type Default = { + name?: string; +}; + +" +`; + exports[`v2 should generate: test/generated/v2/models/DictionaryWithArray.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ @@ -1469,20 +1469,6 @@ export type SimpleStringWithPattern = string; " `; -exports[`v2 should generate: test/generated/v2/schemas/$_default.ts 1`] = ` -"/* istanbul ignore file */ -/* tslint:disable */ -/* eslint-disable */ -export const $_default = { - properties: { - name: { - type: 'string', - }, - }, -} as const; -" -`; - exports[`v2 should generate: test/generated/v2/schemas/$ArrayWithArray.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ @@ -1651,6 +1637,20 @@ export const $Date = { " `; +exports[`v2 should generate: test/generated/v2/schemas/$Default.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $Default = { + properties: { + name: { + type: 'string', + }, + }, +} as const; +" +`; + exports[`v2 should generate: test/generated/v2/schemas/$DictionaryWithArray.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ From 34fa15dbf62ae64d4f15369deee0868759bac0c8 Mon Sep 17 00:00:00 2001 From: Theo Mathieu Date: Tue, 30 May 2023 09:42:04 +0200 Subject: [PATCH 6/7] fix: pascal case --- src/openApi/v3/parser/getType.spec.ts | 18 +++++++++--------- src/openApi/v3/parser/getType.ts | 3 ++- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/openApi/v3/parser/getType.spec.ts b/src/openApi/v3/parser/getType.spec.ts index 06e23f374..32c664da4 100644 --- a/src/openApi/v3/parser/getType.spec.ts +++ b/src/openApi/v3/parser/getType.spec.ts @@ -57,28 +57,28 @@ describe('getType', () => { it('should support dot', () => { const type = getType('#/components/schemas/model.000'); - expect(type.type).toEqual('model_000'); - expect(type.base).toEqual('model_000'); + expect(type.type).toEqual('Model000'); + expect(type.base).toEqual('Model000'); expect(type.template).toEqual(null); - expect(type.imports).toEqual(['model_000']); + expect(type.imports).toEqual(['Model000']); expect(type.isNullable).toEqual(false); }); it('should support dashes', () => { const type = getType('#/components/schemas/some_special-schema'); - expect(type.type).toEqual('some_special_schema'); - expect(type.base).toEqual('some_special_schema'); + expect(type.type).toEqual('SomeSpecialSchema'); + expect(type.base).toEqual('SomeSpecialSchema'); expect(type.template).toEqual(null); - expect(type.imports).toEqual(['some_special_schema']); + expect(type.imports).toEqual(['SomeSpecialSchema']); expect(type.isNullable).toEqual(false); }); it('should support dollar sign', () => { const type = getType('#/components/schemas/$some+special+schema'); - expect(type.type).toEqual('$some_special_schema'); - expect(type.base).toEqual('$some_special_schema'); + expect(type.type).toEqual('$SomeSpecialSchema'); + expect(type.base).toEqual('$SomeSpecialSchema'); expect(type.template).toEqual(null); - expect(type.imports).toEqual(['$some_special_schema']); + expect(type.imports).toEqual(['$SomeSpecialSchema']); expect(type.isNullable).toEqual(false); }); diff --git a/src/openApi/v3/parser/getType.ts b/src/openApi/v3/parser/getType.ts index e8ef4733d..c3415ed8e 100644 --- a/src/openApi/v3/parser/getType.ts +++ b/src/openApi/v3/parser/getType.ts @@ -1,5 +1,6 @@ import type { Type } from '../../../client/interfaces/Type'; import { isDefined } from '../../../utils/isDefined'; +import { toPascalCase } from '../../../utils/toPascalCase'; import { getMappedType } from './getMappedType'; import { stripNamespace } from './stripNamespace'; @@ -71,7 +72,7 @@ export const getType = (type: string | string[] = 'any', format?: string): Type } if (typeWithoutNamespace) { - const type = encode(typeWithoutNamespace); + const type = toPascalCase(encode(typeWithoutNamespace)); result.type = type; result.base = type; result.imports.push(type); From 9adcdd6e297e09bc8ae63b90db4c39f16c608be0 Mon Sep 17 00:00:00 2001 From: Theo Mathieu Date: Tue, 30 May 2023 18:54:26 +0200 Subject: [PATCH 7/7] fix: wip --- Dockerfile | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index d087c578b..7bb87a02e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node:alpine +FROM node:alpine AS builder WORKDIR /usr/src/openapi COPY ["package.json", "package-lock.json", "./"] @@ -6,5 +6,13 @@ RUN ["npm", "install"] COPY . /usr/src/openapi RUN npm run release + +FROM node:alpine +WORKDIR /usr/src/openapi +COPY ["package.json", "package-lock.json", "./"] +RUN npm install --production +COPY --from=builder /usr/src/openapi/dist/index.js /usr/src/openapi/dist/index.js +COPY --from=builder /usr/src/openapi/bin/index.js /usr/src/openapi/bin/index.js ENTRYPOINT [ "node", "/usr/src/openapi/bin/index.js" ] CMD "--help" +