Skip to content

Commit

Permalink
Merge pull request #417 from hey-api/fix/unhandled-syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos authored Apr 18, 2024
2 parents fa40f51 + 886b222 commit 6f065f9
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-suns-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: unhandled SyntaxKind unknown when specification has numeric enums
21 changes: 14 additions & 7 deletions packages/openapi-ts/src/compiler/typedef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,13 @@ export enum ModelWithNestedArrayEnumsDataBarEnum {
BAZ = 'baz',
QUX = 'qux',
}

/**
* Период
*/
export enum ValueEnum {
'_1' = 1,
'_3' = 3,
'_6' = 6,
'_12' = 12,
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
6 changes: 6 additions & 0 deletions packages/openapi-ts/test/spec/v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -3157,6 +3157,12 @@
},
"minItems": 2,
"maxItems": 2
},
"ModelWithNumericEnumUnion": {
"type": "object",
"properties": {
"value": { "type": "number", "description": "Период", "enum": [1, 3, 6, 12] }
}
}
}
}
Expand Down

0 comments on commit 6f065f9

Please sign in to comment.