From 886b222ca0d1887a8d138ea93ebcbe0a50fe8ee0 Mon Sep 17 00:00:00 2001 From: Jordan Shatford Date: Thu, 18 Apr 2024 16:16:40 +1000 Subject: [PATCH] fix: unhandled SyntaxKind unknown when specification has numeric enums --- .changeset/fluffy-suns-exist.md | 5 +++++ packages/openapi-ts/src/compiler/typedef.ts | 21 ++++++++++++------- .../test/generated/v3/enums.gen.ts.snap | 10 +++++++++ .../test/generated/v3/schemas.gen.ts.snap | 11 ++++++++++ .../test/generated/v3/types.gen.ts.snap | 7 +++++++ .../generated/v3_angular/schemas.gen.ts.snap | 11 ++++++++++ .../generated/v3_angular/types.gen.ts.snap | 7 +++++++ .../generated/v3_client/enums.gen.ts.snap | 10 +++++++++ .../generated/v3_client/types.gen.ts.snap | 7 +++++++ .../generated/v3_date/schemas.gen.ts.snap | 11 ++++++++++ .../v3_enums_typescript/enums.gen.ts.snap | 10 +++++++++ .../v3_enums_typescript/schemas.gen.ts.snap | 11 ++++++++++ .../v3_enums_typescript/types.gen.ts.snap | 7 +++++++ .../generated/v3_models/types.gen.ts.snap | 7 +++++++ packages/openapi-ts/test/spec/v3.json | 6 ++++++ 15 files changed, 134 insertions(+), 7 deletions(-) create mode 100644 .changeset/fluffy-suns-exist.md diff --git a/.changeset/fluffy-suns-exist.md b/.changeset/fluffy-suns-exist.md new file mode 100644 index 000000000..7514344fb --- /dev/null +++ b/.changeset/fluffy-suns-exist.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": patch +--- + +fix: unhandled SyntaxKind unknown when specification has numeric enums diff --git a/packages/openapi-ts/src/compiler/typedef.ts b/packages/openapi-ts/src/compiler/typedef.ts index 34cda0be1..30602b6d2 100644 --- a/packages/openapi-ts/src/compiler/typedef.ts +++ b/packages/openapi-ts/src/compiler/typedef.ts @@ -2,13 +2,20 @@ import ts from 'typescript'; import { addLeadingComment, type Comments, tsNodeToString } from './utils'; -export const createTypeNode = (base: any | ts.TypeNode, args?: (any | ts.TypeNode)[]): ts.TypeNode => - ts.isTypeNode(base) - ? base - : ts.factory.createTypeReferenceNode( - base, - args?.map(arg => createTypeNode(arg)) - ); +export const createTypeNode = (base: any | ts.TypeNode, args?: (any | ts.TypeNode)[]): ts.TypeNode => { + if (ts.isTypeNode(base)) { + return base; + } + + if (typeof base === 'number') { + return ts.factory.createLiteralTypeNode(ts.factory.createNumericLiteral(base)); + } + + return ts.factory.createTypeReferenceNode( + base, + args?.map(arg => createTypeNode(arg)) + ); +}; /** * Create a type alias declaration. Example `export type X = Y;`. 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 5c355399f..a1c7b9290 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 @@ -109,3 +109,13 @@ export const ModelWithNestedArrayEnumsDataBarEnum = { BAZ: 'baz', QUX: 'qux', } as const; + +/** + * Период + */ +export const ValueEnum = { + _1: 1, + _3: 3, + _6: 6, + _12: 12, +} as const; 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 3b01687ef..700e86e34 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 @@ -1630,6 +1630,17 @@ export const $ModelWithAnyOfConstantSizeArrayAndIntersect = { maxItems: 2, } as const; +export const $ModelWithNumericEnumUnion = { + type: 'object', + properties: { + value: { + type: 'number', + description: 'Период', + enum: [1, 3, 6, 12], + }, + }, +} as const; + export const $SimpleParameter = { description: 'This is a reusable parameter', name: 'parameter', 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 7d121eb8e..d6f219c2a 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 @@ -786,6 +786,13 @@ export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [number | strin export type ModelWithAnyOfConstantSizeArrayAndIntersect = [number & string, number & string]; +export type ModelWithNumericEnumUnion = { + /** + * Период + */ + value?: 1 | 3 | 6 | 12; +}; + /** * This is a reusable parameter */ diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.gen.ts.snap index 3b01687ef..700e86e34 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.gen.ts.snap @@ -1630,6 +1630,17 @@ export const $ModelWithAnyOfConstantSizeArrayAndIntersect = { maxItems: 2, } as const; +export const $ModelWithNumericEnumUnion = { + type: 'object', + properties: { + value: { + type: 'number', + description: 'Период', + enum: [1, 3, 6, 12], + }, + }, +} as const; + export const $SimpleParameter = { description: 'This is a reusable parameter', name: 'parameter', 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 7d121eb8e..d6f219c2a 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 @@ -786,6 +786,13 @@ export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [number | strin export type ModelWithAnyOfConstantSizeArrayAndIntersect = [number & string, number & string]; +export type ModelWithNumericEnumUnion = { + /** + * Период + */ + value?: 1 | 3 | 6 | 12; +}; + /** * This is a reusable parameter */ 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 5c355399f..a1c7b9290 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 @@ -109,3 +109,13 @@ export const ModelWithNestedArrayEnumsDataBarEnum = { BAZ: 'baz', QUX: 'qux', } as const; + +/** + * Период + */ +export const ValueEnum = { + _1: 1, + _3: 3, + _6: 6, + _12: 12, +} as const; 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 03ae00d3a..ea3f1f483 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 @@ -786,6 +786,13 @@ export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [number | strin export type ModelWithAnyOfConstantSizeArrayAndIntersect = [number & string, number & string]; +export type ModelWithNumericEnumUnion = { + /** + * Период + */ + value?: 1 | 3 | 6 | 12; +}; + /** * This is a reusable parameter */ diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/schemas.gen.ts.snap index 3b01687ef..700e86e34 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_date/schemas.gen.ts.snap @@ -1630,6 +1630,17 @@ export const $ModelWithAnyOfConstantSizeArrayAndIntersect = { maxItems: 2, } as const; +export const $ModelWithNumericEnumUnion = { + type: 'object', + properties: { + value: { + type: 'number', + description: 'Период', + enum: [1, 3, 6, 12], + }, + }, +} as const; + export const $SimpleParameter = { description: 'This is a reusable parameter', name: 'parameter', 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 dfb108b6e..12ff9012a 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 @@ -109,3 +109,13 @@ export enum ModelWithNestedArrayEnumsDataBarEnum { BAZ = 'baz', QUX = 'qux', } + +/** + * Период + */ +export enum ValueEnum { + '_1' = 1, + '_3' = 3, + '_6' = 6, + '_12' = 12, +} diff --git a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/schemas.gen.ts.snap b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/schemas.gen.ts.snap index 3b01687ef..700e86e34 100644 --- a/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/schemas.gen.ts.snap +++ b/packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/schemas.gen.ts.snap @@ -1630,6 +1630,17 @@ export const $ModelWithAnyOfConstantSizeArrayAndIntersect = { maxItems: 2, } as const; +export const $ModelWithNumericEnumUnion = { + type: 'object', + properties: { + value: { + type: 'number', + description: 'Период', + enum: [1, 3, 6, 12], + }, + }, +} as const; + export const $SimpleParameter = { description: 'This is a reusable parameter', name: 'parameter', 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 7d121eb8e..d6f219c2a 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 @@ -786,6 +786,13 @@ export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [number | strin export type ModelWithAnyOfConstantSizeArrayAndIntersect = [number & string, number & string]; +export type ModelWithNumericEnumUnion = { + /** + * Период + */ + value?: 1 | 3 | 6 | 12; +}; + /** * This is a reusable parameter */ 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 cabc93c3a..30e151c87 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 @@ -786,6 +786,13 @@ export type ModelWithAnyOfConstantSizeArrayWithNSizeAndOptions = [number | strin export type ModelWithAnyOfConstantSizeArrayAndIntersect = [number & string, number & string]; +export type ModelWithNumericEnumUnion = { + /** + * Период + */ + value?: 1 | 3 | 6 | 12; +}; + /** * This is a reusable parameter */ diff --git a/packages/openapi-ts/test/spec/v3.json b/packages/openapi-ts/test/spec/v3.json index ff09ebf3a..99aaad2df 100644 --- a/packages/openapi-ts/test/spec/v3.json +++ b/packages/openapi-ts/test/spec/v3.json @@ -3157,6 +3157,12 @@ }, "minItems": 2, "maxItems": 2 + }, + "ModelWithNumericEnumUnion": { + "type": "object", + "properties": { + "value": { "type": "number", "description": "Период", "enum": [1, 3, 6, 12] } + } } } }