diff --git a/CHANGELOG.md b/CHANGELOG.md index 833f82b9..80a3666f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. ### Fixed - Support enums with single quotes in names - Generating better names when `operationId` is not given (breaking change) +- Fixed issue where `x-enum` flags where breaking due to non-string values ## [0.19.0] - 2022-02-02 ### Added diff --git a/src/openApi/v2/parser/extendEnum.ts b/src/openApi/v2/parser/extendEnum.ts index 0a5ebeb1..c41dcd76 100644 --- a/src/openApi/v2/parser/extendEnum.ts +++ b/src/openApi/v2/parser/extendEnum.ts @@ -1,4 +1,5 @@ import type { Enum } from '../../../client/interfaces/Enum'; +import { isString } from '../../../utils/isString'; import type { WithEnumExtension } from '../interfaces/Extensions/WithEnumExtension'; /** @@ -8,12 +9,12 @@ import type { WithEnumExtension } from '../interfaces/Extensions/WithEnumExtensi * @param definition */ export const extendEnum = (enumerators: Enum[], definition: WithEnumExtension): Enum[] => { - const names = definition['x-enum-varnames']; - const descriptions = definition['x-enum-descriptions']; + const names = definition['x-enum-varnames']?.filter(isString); + const descriptions = definition['x-enum-descriptions']?.filter(isString); return enumerators.map((enumerator, index) => ({ name: names?.[index] || enumerator.name, - description: descriptions?.[index] || enumerator.description, + description: JSON.stringify(descriptions?.[index] || enumerator.description), value: enumerator.value, type: enumerator.type, })); diff --git a/src/openApi/v3/parser/extendEnum.ts b/src/openApi/v3/parser/extendEnum.ts index 0a5ebeb1..a7fa865c 100644 --- a/src/openApi/v3/parser/extendEnum.ts +++ b/src/openApi/v3/parser/extendEnum.ts @@ -1,4 +1,5 @@ import type { Enum } from '../../../client/interfaces/Enum'; +import { isString } from '../../../utils/isString'; import type { WithEnumExtension } from '../interfaces/Extensions/WithEnumExtension'; /** @@ -8,8 +9,8 @@ import type { WithEnumExtension } from '../interfaces/Extensions/WithEnumExtensi * @param definition */ export const extendEnum = (enumerators: Enum[], definition: WithEnumExtension): Enum[] => { - const names = definition['x-enum-varnames']; - const descriptions = definition['x-enum-descriptions']; + const names = definition['x-enum-varnames']?.filter(isString); + const descriptions = definition['x-enum-descriptions']?.filter(isString); return enumerators.map((enumerator, index) => ({ name: names?.[index] || enumerator.name, diff --git a/src/templates/partials/exportEnum.hbs b/src/templates/partials/exportEnum.hbs index 2b28749b..bcaeb24e 100644 --- a/src/templates/partials/exportEnum.hbs +++ b/src/templates/partials/exportEnum.hbs @@ -11,7 +11,7 @@ export enum {{{name}}} { */ {{/if}} {{#containsSpaces name}} - "{{{name}}}" = {{{value}}}, + '{{{name}}}' = {{{value}}}, {{else}} {{{name}}} = {{{value}}}, {{/containsSpaces}} diff --git a/test/__snapshots__/index.spec.ts.snap b/test/__snapshots__/index.spec.ts.snap index b8016e0d..580f451a 100644 --- a/test/__snapshots__/index.spec.ts.snap +++ b/test/__snapshots__/index.spec.ts.snap @@ -890,15 +890,15 @@ exports[`v2 should generate: ./test/generated/v2/models/EnumWithExtensions.ts 1` */ export enum EnumWithExtensions { /** - * Used when the status of something is successful + * \\"Used when the status of something is successful\\" */ CUSTOM_SUCCESS = 200, /** - * Used when the status of something has a warning + * \\"Used when the status of something has a warning\\" */ CUSTOM_WARNING = 400, /** - * Used when the status of something has an error + * \\"Used when the status of something has an error\\" */ CUSTOM_ERROR = 500, }" @@ -913,20 +913,65 @@ exports[`v2 should generate: ./test/generated/v2/models/EnumWithNumbers.ts 1`] = * This is a simple enum with numbers */ export enum EnumWithNumbers { + /** + * null + */ '_1' = 1, + /** + * null + */ '_2' = 2, + /** + * null + */ '_3' = 3, + /** + * null + */ '_1.1' = 1.1, + /** + * null + */ '_1.2' = 1.2, + /** + * null + */ '_1.3' = 1.3, + /** + * null + */ '_100' = 100, + /** + * null + */ '_200' = 200, + /** + * null + */ '_300' = 300, + /** + * null + */ '_-100' = -100, + /** + * null + */ '_-200' = -200, + /** + * null + */ '_-300' = -300, + /** + * null + */ '_-1.1' = -1.1, + /** + * null + */ '_-1.2' = -1.2, + /** + * null + */ '_-1.3' = -1.3, }" `; @@ -940,10 +985,25 @@ exports[`v2 should generate: ./test/generated/v2/models/EnumWithStrings.ts 1`] = * This is a simple enum with strings */ export enum EnumWithStrings { + /** + * null + */ SUCCESS = 'Success', + /** + * null + */ WARNING = 'Warning', + /** + * null + */ ERROR = 'Error', + /** + * null + */ _SINGLE_QUOTE_ = ''Single Quote'', + /** + * null + */ _DOUBLE_QUOTES_ = '\\"Double Quotes\\"', }" `;