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); })