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..7bb87a02e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,18 @@ -FROM node:alpine +FROM node:alpine AS builder WORKDIR /usr/src/openapi + +COPY ["package.json", "package-lock.json", "./"] +RUN ["npm", "install"] + COPY . /usr/src/openapi -RUN npm install 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" + 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/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); 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); 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, ''); +}; 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 */