From a5d7c6f63810495eb950b0bf7917b5905943bd99 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Date: Wed, 21 Aug 2024 17:55:26 +0200 Subject: [PATCH] fix: Correctly handle negative numbers, fix #65 (#67) --- .yarn/versions/66b0f787.yml | 5 +++++ .../utils/__tests__/__snapshots__/types.spec.ts.snap | 2 ++ .../src/generator/utils/__tests__/types.spec.ts | 8 ++++++++ packages/typoas-generator/src/generator/utils/types.ts | 9 ++++++++- 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 .yarn/versions/66b0f787.yml diff --git a/.yarn/versions/66b0f787.yml b/.yarn/versions/66b0f787.yml new file mode 100644 index 0000000..8276a61 --- /dev/null +++ b/.yarn/versions/66b0f787.yml @@ -0,0 +1,5 @@ +releases: + "@typoas/generator": patch + +declined: + - "@typoas/cli" diff --git a/packages/typoas-generator/src/generator/utils/__tests__/__snapshots__/types.spec.ts.snap b/packages/typoas-generator/src/generator/utils/__tests__/__snapshots__/types.spec.ts.snap index a2e94a3..eea6436 100644 --- a/packages/typoas-generator/src/generator/utils/__tests__/__snapshots__/types.spec.ts.snap +++ b/packages/typoas-generator/src/generator/utils/__tests__/__snapshots__/types.spec.ts.snap @@ -30,6 +30,8 @@ exports[`create type from schema should handle primitive boolean enums 1`] = `"t exports[`create type from schema should handle primitive boolean schema 1`] = `"boolean"`; +exports[`create type from schema should handle primitive negative number enums 1`] = `"-1 | 6"`; + exports[`create type from schema should handle primitive number enums 1`] = `"1 | 6"`; exports[`create type from schema should handle primitive number schema 1`] = `"number"`; diff --git a/packages/typoas-generator/src/generator/utils/__tests__/types.spec.ts b/packages/typoas-generator/src/generator/utils/__tests__/types.spec.ts index cee2735..dfc63f1 100644 --- a/packages/typoas-generator/src/generator/utils/__tests__/types.spec.ts +++ b/packages/typoas-generator/src/generator/utils/__tests__/types.spec.ts @@ -78,6 +78,14 @@ describe('create type from schema', () => { expect(getStringFromNode(node)).toMatchSnapshot(); }); + it('should handle primitive negative number enums', () => { + const schema: SchemaObject = { type: 'number', enum: [-1, 6] }; + + const node = createTypeFromSchema(schema, new Context()); + + expect(getStringFromNode(node)).toMatchSnapshot(); + }); + it('should handle dates', () => { const schema: SchemaObject = { type: 'string', format: 'date-time' }; diff --git a/packages/typoas-generator/src/generator/utils/types.ts b/packages/typoas-generator/src/generator/utils/types.ts index 738f180..f10d7db 100644 --- a/packages/typoas-generator/src/generator/utils/types.ts +++ b/packages/typoas-generator/src/generator/utils/types.ts @@ -60,7 +60,14 @@ export function createTypeFromSchema( return e ? factory.createTrue() : factory.createFalse(); } if (typeof e === 'number') { - return factory.createNumericLiteral(e); + const literal = factory.createNumericLiteral(Math.abs(e)); + if (e >= 0) { + return literal; + } + return factory.createPrefixUnaryExpression( + SyntaxKind.MinusToken, + literal, + ); } return factory.createStringLiteral(e as string, true); })