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

Fixed TypeResolver issue for negative number literal types #1734

Merged
merged 1 commit into from
Jan 27, 2025
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
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
Loading