Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correctly handle negative numbers, fix #65 #67

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .yarn/versions/66b0f787.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
releases:
"@typoas/generator": patch

declined:
- "@typoas/cli"
Original file line number Diff line number Diff line change
Expand Up @@ -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"`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' };

Expand Down
9 changes: 8 additions & 1 deletion packages/typoas-generator/src/generator/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand Down