diff --git a/src/openApi/v2/parser/getEnum.ts b/src/openApi/v2/parser/getEnum.ts index 83102fec..858c7ef3 100644 --- a/src/openApi/v2/parser/getEnum.ts +++ b/src/openApi/v2/parser/getEnum.ts @@ -10,14 +10,18 @@ export function getEnum(values?: (string | number)[]): Enum[] { .map(value => { if (typeof value === 'number') { return { - name: `NUM_${value}`, + name: `$${value}`, value: String(value), type: PrimaryType.NUMBER, description: null, }; } return { - name: value.replace(/([a-z])([A-Z]+)/g, '$1_$2').toUpperCase(), + name: value + .replace(/\W+/g, '_') + .replace(/^(\d+)/g, s => `$${s}`) + .replace(/([a-z])([A-Z]+)/g, '$1_$2') + .toUpperCase(), value: `'${value}'`, type: PrimaryType.STRING, description: null, diff --git a/src/openApi/v2/parser/getEnumFromDescription.ts b/src/openApi/v2/parser/getEnumFromDescription.ts index 72e21c94..6020239f 100644 --- a/src/openApi/v2/parser/getEnumFromDescription.ts +++ b/src/openApi/v2/parser/getEnumFromDescription.ts @@ -14,7 +14,11 @@ export function getEnumFromDescription(description: string): Enum[] { const value = parseInt(match.split('=')[1].replace(/[^0-9]/g, '')); if (name && Number.isInteger(value)) { symbols.push({ - name: name.replace(/([a-z])([A-Z]+)/g, '$1_$2').toUpperCase(), + name: name + .replace(/\W+/g, '_') + .replace(/^(\d+)/g, s => `$${s}`) + .replace(/([a-z])([A-Z]+)/g, '$1_$2') + .toUpperCase(), value: String(value), type: PrimaryType.NUMBER, description: null, diff --git a/src/openApi/v3/parser/getEnum.ts b/src/openApi/v3/parser/getEnum.ts index 83102fec..858c7ef3 100644 --- a/src/openApi/v3/parser/getEnum.ts +++ b/src/openApi/v3/parser/getEnum.ts @@ -10,14 +10,18 @@ export function getEnum(values?: (string | number)[]): Enum[] { .map(value => { if (typeof value === 'number') { return { - name: `NUM_${value}`, + name: `$${value}`, value: String(value), type: PrimaryType.NUMBER, description: null, }; } return { - name: value.replace(/([a-z])([A-Z]+)/g, '$1_$2').toUpperCase(), + name: value + .replace(/\W+/g, '_') + .replace(/^(\d+)/g, s => `$${s}`) + .replace(/([a-z])([A-Z]+)/g, '$1_$2') + .toUpperCase(), value: `'${value}'`, type: PrimaryType.STRING, description: null, diff --git a/src/openApi/v3/parser/getEnumFromDescription.ts b/src/openApi/v3/parser/getEnumFromDescription.ts index 72e21c94..6020239f 100644 --- a/src/openApi/v3/parser/getEnumFromDescription.ts +++ b/src/openApi/v3/parser/getEnumFromDescription.ts @@ -14,7 +14,11 @@ export function getEnumFromDescription(description: string): Enum[] { const value = parseInt(match.split('=')[1].replace(/[^0-9]/g, '')); if (name && Number.isInteger(value)) { symbols.push({ - name: name.replace(/([a-z])([A-Z]+)/g, '$1_$2').toUpperCase(), + name: name + .replace(/\W+/g, '_') + .replace(/^(\d+)/g, s => `$${s}`) + .replace(/([a-z])([A-Z]+)/g, '$1_$2') + .toUpperCase(), value: String(value), type: PrimaryType.NUMBER, description: null, diff --git a/src/utils/writeClient.ts b/src/utils/writeClient.ts index e9fcf189..e50c3672 100644 --- a/src/utils/writeClient.ts +++ b/src/utils/writeClient.ts @@ -55,7 +55,6 @@ export function writeClient( copySupportFile('core/getFormData.ts', outputPath); copySupportFile('core/getQueryString.ts', outputPath); copySupportFile('core/isSuccess.ts', outputPath); - copySupportFile('core/OpenAPI.hbs', outputPath); copySupportFile('core/request.ts', outputPath); copySupportFile('core/RequestOptions.ts', outputPath); copySupportFile('core/requestUsingFetch.ts', outputPath); diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index c28bedfc..bef686a9 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -791,9 +791,9 @@ exports[`generation v2 file(./test/result/v2/models/EnumWithNumbers.ts): ./test/ * This is a simple enum with numbers */ export enum EnumWithNumbers { - NUM_1 = 1, - NUM_2 = 2, - NUM_3 = 3, + $1 = 1, + $2 = 2, + $3 = 3, }" `; @@ -996,6 +996,10 @@ export interface ModelWithEnum { * This is a simple enum with strings */ test?: ModelWithEnum.test; + /** + * These are the HTTP error code enums + */ + statusCode?: ModelWithEnum.statusCode; } export namespace ModelWithEnum { @@ -1009,6 +1013,18 @@ export namespace ModelWithEnum { ERROR = 'Error', } + /** + * These are the HTTP error code enums + */ + export enum statusCode { + $100 = '100', + $200_FOO = '200 FOO', + $300_FOO_BAR = '300 FOO_BAR', + $400_FOO_BAR = '400 foo-bar', + $500_FOO_BAR = '500 foo.bar', + $600_FOO_BAR = '600 foo&bar', + } + } " @@ -1640,6 +1656,9 @@ export const $ModelWithEnum = { test: { type: 'Enum', }, + statusCode: { + type: 'Enum', + }, }, };" `; @@ -3413,9 +3432,9 @@ exports[`generation v3 file(./test/result/v3/models/EnumWithNumbers.ts): ./test/ * This is a simple enum with numbers */ export enum EnumWithNumbers { - NUM_1 = 1, - NUM_2 = 2, - NUM_3 = 3, + $1 = 1, + $2 = 2, + $3 = 3, }" `; @@ -3639,7 +3658,11 @@ export interface ModelWithEnum { /** * This is a simple enum with strings */ - Test?: ModelWithEnum.Test; + test?: ModelWithEnum.test; + /** + * These are the HTTP error code enums + */ + statusCode?: ModelWithEnum.statusCode; } export namespace ModelWithEnum { @@ -3647,12 +3670,24 @@ export namespace ModelWithEnum { /** * This is a simple enum with strings */ - export enum Test { + export enum test { SUCCESS = 'Success', WARNING = 'Warning', ERROR = 'Error', } + /** + * These are the HTTP error code enums + */ + export enum statusCode { + $100 = '100', + $200_FOO = '200 FOO', + $300_FOO_BAR = '300 FOO_BAR', + $400_FOO_BAR = '400 foo-bar', + $500_FOO_BAR = '500 foo.bar', + $600_FOO_BAR = '600 foo&bar', + } + } " @@ -3672,7 +3707,7 @@ export interface ModelWithEnumFromDescription { /** * Success=1,Warning=2,Error=3 */ - Test?: ModelWithEnumFromDescription.Test; + test?: ModelWithEnumFromDescription.test; } export namespace ModelWithEnumFromDescription { @@ -3680,7 +3715,7 @@ export namespace ModelWithEnumFromDescription { /** * Success=1,Warning=2,Error=3 */ - export enum Test { + export enum test { SUCCESS = 1, WARNING = 2, ERROR = 3, @@ -4319,7 +4354,10 @@ exports[`generation v3 file(./test/result/v3/schemas/$ModelWithEnum.ts): ./test/ export const $ModelWithEnum = { properties: { - Test: { + test: { + type: 'Enum', + }, + statusCode: { type: 'Enum', }, }, @@ -4334,7 +4372,7 @@ exports[`generation v3 file(./test/result/v3/schemas/$ModelWithEnumFromDescripti export const $ModelWithEnumFromDescription = { properties: { - Test: { + test: { type: 'Enum', }, }, diff --git a/test/mock/v2/spec.json b/test/mock/v2/spec.json index fac43da5..caa60e68 100644 --- a/test/mock/v2/spec.json +++ b/test/mock/v2/spec.json @@ -862,6 +862,17 @@ "Warning", "Error" ] + }, + "statusCode": { + "description": "These are the HTTP error code enums", + "enum": [ + "100", + "200 FOO", + "300 FOO_BAR", + "400 foo-bar", + "500 foo.bar", + "600 foo&bar" + ] } } }, diff --git a/test/mock/v3/spec.json b/test/mock/v3/spec.json index 94ef2c18..b740dc4f 100644 --- a/test/mock/v3/spec.json +++ b/test/mock/v3/spec.json @@ -1304,13 +1304,24 @@ "description": "This is a model with one enum", "type": "object", "properties": { - "Test": { + "test": { "description": "This is a simple enum with strings", "enum": [ "Success", "Warning", "Error" ] + }, + "statusCode": { + "description": "These are the HTTP error code enums", + "enum": [ + "100", + "200 FOO", + "300 FOO_BAR", + "400 foo-bar", + "500 foo.bar", + "600 foo&bar" + ] } } }, @@ -1318,7 +1329,7 @@ "description": "This is a model with one enum", "type": "object", "properties": { - "Test": { + "test": { "type": "integer", "description": "Success=1,Warning=2,Error=3" }