Skip to content

Commit

Permalink
Merge pull request #470 from hey-api/fix/negative-enum-numeric-values
Browse files Browse the repository at this point in the history
fix: negative numbers in numeric enums
  • Loading branch information
jordanshatford authored Apr 23, 2024
2 parents dfad425 + 520318f commit ab68dfa
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-radios-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: negative numbers in numeric enums
11 changes: 7 additions & 4 deletions packages/openapi-ts/src/compiler/typedef.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ export const ModelWithNestedArrayEnumsDataBarEnum = {
* Период
*/
export const ValueEnum = {
'_-10': -10,
'_-1': -1,
'_0': 0,
'_1': 1,
'_3': 3,
'_6': 6,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ export type ModelWithNumericEnumUnion = {
/**
* Период
*/
value?: 1 | 3 | 6 | 12;
value?: -10 | -1 | 0 | 1 | 3 | 6 | 12;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ export type ModelWithNumericEnumUnion = {
/**
* Период
*/
value?: 1 | 3 | 6 | 12;
value?: -10 | -1 | 0 | 1 | 3 | 6 | 12;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ export const ModelWithNestedArrayEnumsDataBarEnum = {
* Период
*/
export const ValueEnum = {
'_-10': -10,
'_-1': -1,
'_0': 0,
'_1': 1,
'_3': 3,
'_6': 6,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ export type ModelWithNumericEnumUnion = {
/**
* Период
*/
value?: 1 | 3 | 6 | 12;
value?: -10 | -1 | 0 | 1 | 3 | 6 | 12;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ export enum ModelWithNestedArrayEnumsDataBarEnum {
* Период
*/
export enum ValueEnum {
'_-10' = -10,
'_-1' = -1,
'_0' = 0,
'_1' = 1,
'_3' = 3,
'_6' = 6,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ export type ModelWithNumericEnumUnion = {
/**
* Период
*/
value?: 1 | 3 | 6 | 12;
value?: -10 | -1 | 0 | 1 | 3 | 6 | 12;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ export type ModelWithNumericEnumUnion = {
/**
* Период
*/
value?: 1 | 3 | 6 | 12;
value?: -10 | -1 | 0 | 1 | 3 | 6 | 12;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/test/spec/v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -3190,7 +3190,7 @@
"value": {
"type": "number",
"description": "Период",
"enum": [1, 3, 6, 12]
"enum": [-10, -1, 0, 1, 3, 6, 12]
}
}
},
Expand Down

0 comments on commit ab68dfa

Please sign in to comment.