From 520318f1c2c8ab91a363fae2c2a7c6f2f502b47c Mon Sep 17 00:00:00 2001 From: Jordan Shatford Date: Tue, 23 Apr 2024 09:49:19 +1000 Subject: [PATCH] fix: negative numbers in numeric enums --- .changeset/early-radios-sort.md | 5 +++++ packages/openapi-ts/src/compiler/typedef.ts | 11 +++++++---- .../__snapshots__/test/generated/v3/enums.gen.ts.snap | 3 +++ .../test/generated/v3/schemas.gen.ts.snap | 2 +- .../__snapshots__/test/generated/v3/types.gen.ts.snap | 2 +- .../test/generated/v3_angular/types.gen.ts.snap | 2 +- .../test/generated/v3_client/enums.gen.ts.snap | 3 +++ .../test/generated/v3_client/types.gen.ts.snap | 2 +- .../generated/v3_enums_typescript/enums.gen.ts.snap | 3 +++ .../generated/v3_enums_typescript/types.gen.ts.snap | 2 +- .../test/generated/v3_models/types.gen.ts.snap | 2 +- .../generated/v3_schemas_form/schemas.gen.ts.snap | 2 +- .../generated/v3_schemas_json/schemas.gen.ts.snap | 2 +- packages/openapi-ts/test/spec/v3.json | 2 +- 14 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 .changeset/early-radios-sort.md diff --git a/.changeset/early-radios-sort.md b/.changeset/early-radios-sort.md new file mode 100644 index 000000000..862f2a512 --- /dev/null +++ b/.changeset/early-radios-sort.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": patch +--- + +fix: negative numbers in numeric enums diff --git a/packages/openapi-ts/src/compiler/typedef.ts b/packages/openapi-ts/src/compiler/typedef.ts index 6d6caa07b..31c4549f5 100644 --- a/packages/openapi-ts/src/compiler/typedef.ts +++ b/packages/openapi-ts/src/compiler/typedef.ts @@ -1,6 +1,11 @@ import ts from 'typescript'; -import { addLeadingJSDocComment, type Comments, tsNodeToString } from './utils'; +import { + addLeadingJSDocComment, + type Comments, + ots, + tsNodeToString, +} from './utils'; export const createTypeNode = ( base: any | ts.TypeNode, @@ -11,9 +16,7 @@ export const createTypeNode = ( } if (typeof base === 'number') { - return ts.factory.createLiteralTypeNode( - ts.factory.createNumericLiteral(base), - ); + return ts.factory.createLiteralTypeNode(ots.number(base)); } return ts.factory.createTypeReferenceNode( diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3/enums.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3/enums.gen.ts.snap index 73bd72f36..641bb2c83 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3/enums.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3/enums.gen.ts.snap @@ -114,6 +114,9 @@ export const ModelWithNestedArrayEnumsDataBarEnum = { * Период */ export const ValueEnum = { + '_-10': -10, + '_-1': -1, + '_0': 0, '_1': 1, '_3': 3, '_6': 6, diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.gen.ts.snap index 4af40c30e..5fef0fc25 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.gen.ts.snap @@ -1643,7 +1643,7 @@ export const $ModelWithNumericEnumUnion = { value: { type: 'number', description: 'Период', - enum: [1, 3, 6, 12] + enum: [-10, -1, 0, 1, 3, 6, 12] } } } as const; diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3/types.gen.ts.snap index 5c29a38ea..c3494aaf5 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3/types.gen.ts.snap @@ -770,7 +770,7 @@ export type ModelWithNumericEnumUnion = { /** * Период */ - value?: 1 | 3 | 6 | 12; + value?: -10 | -1 | 0 | 1 | 3 | 6 | 12; }; /** diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/types.gen.ts.snap index 5c29a38ea..c3494aaf5 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/types.gen.ts.snap @@ -770,7 +770,7 @@ export type ModelWithNumericEnumUnion = { /** * Период */ - value?: 1 | 3 | 6 | 12; + value?: -10 | -1 | 0 | 1 | 3 | 6 | 12; }; /** diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/enums.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/enums.gen.ts.snap index 73bd72f36..641bb2c83 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/enums.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/enums.gen.ts.snap @@ -114,6 +114,9 @@ export const ModelWithNestedArrayEnumsDataBarEnum = { * Период */ export const ValueEnum = { + '_-10': -10, + '_-1': -1, + '_0': 0, '_1': 1, '_3': 3, '_6': 6, diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/types.gen.ts.snap index 2e4256553..b5ee34f31 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_client/types.gen.ts.snap @@ -770,7 +770,7 @@ export type ModelWithNumericEnumUnion = { /** * Период */ - value?: 1 | 3 | 6 | 12; + value?: -10 | -1 | 0 | 1 | 3 | 6 | 12; }; /** diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/enums.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/enums.gen.ts.snap index 8482663b5..1e913b0a7 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/enums.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/enums.gen.ts.snap @@ -114,6 +114,9 @@ export enum ModelWithNestedArrayEnumsDataBarEnum { * Период */ export enum ValueEnum { + '_-10' = -10, + '_-1' = -1, + '_0' = 0, '_1' = 1, '_3' = 3, '_6' = 6, diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/types.gen.ts.snap index 5c29a38ea..c3494aaf5 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/types.gen.ts.snap @@ -770,7 +770,7 @@ export type ModelWithNumericEnumUnion = { /** * Период */ - value?: 1 | 3 | 6 | 12; + value?: -10 | -1 | 0 | 1 | 3 | 6 | 12; }; /** diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/types.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/types.gen.ts.snap index adff38410..ef5049da7 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/types.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_models/types.gen.ts.snap @@ -770,7 +770,7 @@ export type ModelWithNumericEnumUnion = { /** * Период */ - value?: 1 | 3 | 6 | 12; + value?: -10 | -1 | 0 | 1 | 3 | 6 | 12; }; /** diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_form/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_form/schemas.gen.ts.snap index 01262c4ef..e780c64d9 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_form/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_form/schemas.gen.ts.snap @@ -1530,7 +1530,7 @@ export const $ModelWithNumericEnumUnion = { properties: { value: { type: 'number', - enum: [1, 3, 6, 12] + enum: [-10, -1, 0, 1, 3, 6, 12] } } } as const; diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_json/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_json/schemas.gen.ts.snap index 4af40c30e..5fef0fc25 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_json/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_schemas_json/schemas.gen.ts.snap @@ -1643,7 +1643,7 @@ export const $ModelWithNumericEnumUnion = { value: { type: 'number', description: 'Период', - enum: [1, 3, 6, 12] + enum: [-10, -1, 0, 1, 3, 6, 12] } } } as const; diff --git a/packages/openapi-ts/test/spec/v3.json b/packages/openapi-ts/test/spec/v3.json index a787c14ec..d695e0c05 100644 --- a/packages/openapi-ts/test/spec/v3.json +++ b/packages/openapi-ts/test/spec/v3.json @@ -3190,7 +3190,7 @@ "value": { "type": "number", "description": "Период", - "enum": [1, 3, 6, 12] + "enum": [-10, -1, 0, 1, 3, 6, 12] } } },