Skip to content

Commit

Permalink
Fixed TypeResolver issue for negative number literal types
Browse files Browse the repository at this point in the history
Also added negativeNumberLiteralType to the tests
Fixes #1733
  • Loading branch information
crycode-de committed Dec 20, 2024
1 parent 95021e8 commit b942878
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/cli/src/metadataGeneration/typeResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,10 @@ export class TypeResolver {
return typeNode.literal.text;
case ts.SyntaxKind.NumericLiteral:
return parseFloat(typeNode.literal.text);
case ts.SyntaxKind.PrefixUnaryExpression:
// make sure to only handle the MinusToken here
throwUnless((typeNode.literal as ts.PrefixUnaryExpression).operator === ts.SyntaxKind.MinusToken, new GenerateMetadataError(`Couldn't resolve literal node: ${typeNode.literal.getText()}`));
return parseFloat(typeNode.literal.getText());
case ts.SyntaxKind.NullKeyword:
return null;
default:
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/testModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface TestModel extends Model {
nullableUnionPrimitiveType?: 'String' | 1 | 20.0 | true | false | null;
undefineableUnionPrimitiveType: 'String' | 1 | 20.0 | true | false | undefined;
singleFloatLiteralType?: 3.1415;
negativeNumberLiteralType?: -1;
dateValue?: Date;
optionalString?: string;
anyType?: any;
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/swagger/definitionsGeneration/definitions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,15 @@ describe('Definition generation', () => {
expect(propertySchema.enum).to.have.length(1, `for property ${propertyName}.enum`);
expect(propertySchema.enum).to.include(3.1415, `for property ${propertyName}.enum`);
},
negativeNumberLiteralType: (propertyName, propertySchema) => {
expect(propertySchema.type).to.eq('number', `for property ${propertyName}.type`);
expect(propertySchema['x-nullable']).to.eq(false, `for property ${propertyName}[x-nullable]`);
if (!propertySchema.enum) {
throw new Error(`There was no 'enum' property on ${propertyName}.`);
}
expect(propertySchema.enum).to.have.length(1, `for property ${propertyName}.enum`);
expect(propertySchema.enum).to.include(-1, `for property ${propertyName}.enum`);
},
dateValue: (propertyName, propertySchema) => {
expect(propertySchema.type).to.eq('string', `for property ${propertyName}.type`);
expect(propertySchema['x-nullable']).to.eq(undefined, `for property ${propertyName}[x-nullable]`);
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/swagger/schemaDetails3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,15 @@ describe('Definition generation for OpenAPI 3.0.0', () => {
expect(propertySchema.enum).to.have.length(1, `for property ${propertyName}.enum`);
expect(propertySchema.enum).to.include(3.1415, `for property ${propertyName}.enum`);
},
negativeNumberLiteralType: (propertyName, propertySchema) => {
expect(propertySchema.type).to.eq('number', `for property ${propertyName}.type`);
expect(propertySchema.nullable).to.eq(false, `for property ${propertyName}.nullable`);
if (!propertySchema.enum) {
throw new Error(`There was no 'enum' property on ${propertyName}.`);
}
expect(propertySchema.enum).to.have.length(1, `for property ${propertyName}.enum`);
expect(propertySchema.enum).to.include(-1, `for property ${propertyName}.enum`);
},
dateValue: (propertyName, propertySchema) => {
expect(propertySchema.type).to.eq('string', `for property ${propertyName}.type`);
expect(propertySchema.nullable).to.eq(undefined, `for property ${propertyName}.nullable`);
Expand Down

0 comments on commit b942878

Please sign in to comment.