Skip to content

Commit

Permalink
handle negative numbers correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
sakari committed Apr 10, 2024
1 parent 26d9da0 commit 86ad1f5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/oats/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@
"koa-router": "12.0.0",
"ts-jest": "29.1.0",
"ts-node": "10.9.1",
"typescript": "4.9.5"
"typescript": "^5.4.4"
}
}
27 changes: 18 additions & 9 deletions packages/oats/src/generate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,19 @@ export function run(options: Options) {
if (e === null) return ts.factory.createNull();
if (type === 'string') return ts.factory.createStringLiteral(e);
if (type === 'bigint') return ts.factory.createBigIntLiteral(e);
if (type === 'number') return ts.factory.createNumericLiteral(e);

if (type === 'number') return generateNumericLiteral(e);
throw new Error(`unsupported enum value: "${e}"`);
}
function generateNumericLiteral(value: number | string): ts.LiteralTypeNode['literal'] {
value = Number(value);
if (value < 0) {
return ts.factory.createPrefixUnaryExpression(
ts.SyntaxKind.MinusToken,
ts.factory.createNumericLiteral(Math.abs(value))
);
}
return ts.factory.createNumericLiteral(value);
}

function generateType(
schema: oautil.SchemaObject,
Expand Down Expand Up @@ -883,7 +892,7 @@ export function run(options: Options) {
? [
ts.factory.createPropertyAssignment(
'minLength',
ts.factory.createNumericLiteral(schema.minLength)
generateNumericLiteral(schema.minLength)
)
]
: [];
Expand All @@ -892,7 +901,7 @@ export function run(options: Options) {
? [
ts.factory.createPropertyAssignment(
'maxLength',
ts.factory.createNumericLiteral(schema.maxLength)
generateNumericLiteral(schema.maxLength)
)
]
: [];
Expand All @@ -915,7 +924,7 @@ export function run(options: Options) {
ts.factory.createPropertyAssignment(
'enum',
ts.factory.createArrayLiteralExpression(
schema.enum.map(i => ts.factory.createNumericLiteral('' + i))
schema.enum.map(i => generateNumericLiteral('' + i))
)
)
]
Expand All @@ -928,15 +937,15 @@ export function run(options: Options) {
properties.push(
ts.factory.createPropertyAssignment(
'minimum',
ts.factory.createNumericLiteral(schema.minimum + '')
generateNumericLiteral(schema.minimum + '')
)
);
}
if (schema.maximum != null) {
properties.push(
ts.factory.createPropertyAssignment(
'maximum',
ts.factory.createNumericLiteral(schema.maximum + '')
generateNumericLiteral(schema.maximum + '')
)
);
}
Expand Down Expand Up @@ -976,15 +985,15 @@ export function run(options: Options) {
properties.push(
ts.factory.createPropertyAssignment(
'minItems',
ts.factory.createNumericLiteral(schema.minItems + '')
generateNumericLiteral(schema.minItems + '')
)
);
}
if (schema.maxItems != null) {
properties.push(
ts.factory.createPropertyAssignment(
'maxItems',
ts.factory.createNumericLiteral(schema.maxItems + '')
generateNumericLiteral(schema.maxItems + '')
)
);
}
Expand Down
11 changes: 11 additions & 0 deletions test/negative-limits/driver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { driver } from '@smartlyio/oats';

import * as process from 'process';
process.chdir(__dirname);

driver.generate({
generatedValueClassFile: './tmp/fixture.types.generated.ts',
header: '/* tslint:disable variable-name only-arrow-functions*/',
openapiFilePath: './fixture.yaml',
resolve: driver.compose(driver.generateFile(), driver.localResolve)
});
11 changes: 11 additions & 0 deletions test/negative-limits/fixture.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
openapi: 3.0.0
info:
version: 1.0.0
title: fixtures for runtime test
paths: {}
components:
schemas:
test:
type: number
minimum: -2

0 comments on commit 86ad1f5

Please sign in to comment.