From c92ee38b122677967fea82d5e7aecce8f6fc4b81 Mon Sep 17 00:00:00 2001 From: Clooooode Date: Thu, 6 Oct 2022 13:30:32 +0800 Subject: [PATCH 1/2] test: Add test case to fit lukeautry/tsoa#1289 Add both plus(+) and minus(-) operator @ ExampleDecorator. Apply to both spec2 & 3 test cases. --- .../fixtures/controllers/exampleController.ts | 29 ++++++++++++++++++ tests/unit/swagger/schemaDetails.spec.ts | 22 ++++++++++++++ tests/unit/swagger/schemaDetails3.spec.ts | 30 +++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/tests/fixtures/controllers/exampleController.ts b/tests/fixtures/controllers/exampleController.ts index a7188fc1c..30195af4b 100644 --- a/tests/fixtures/controllers/exampleController.ts +++ b/tests/fixtures/controllers/exampleController.ts @@ -12,6 +12,11 @@ export interface Location { city: string; } +export interface Doc { + id: number; + description: string; +} + @Route('ExampleTest') export class ExampleTestController { /** @@ -222,4 +227,28 @@ export class ExampleTestController { public async responseMultiExamplesWithProduces(): Promise { return 'test 1'; } + + @Example({ + id: -1, + description: 'test doc des', + }) + @Get('ResponseExampleWithMinusOperatorPrefixValue') + public async responseExampleWithMinusOperatorPrefixValue(): Promise { + return { + id: -1, + description: 'test doc des', + }; + } + + @Example({ + id: +1, + description: 'test doc des', + }) + @Get('ResponseExampleWithPlusOperatorPrefixValue') + public async responseExampleWithPlusOperatorPrefixValue(): Promise { + return { + id: 1, + description: 'test doc des', + }; + } } diff --git a/tests/unit/swagger/schemaDetails.spec.ts b/tests/unit/swagger/schemaDetails.spec.ts index 4a8547e28..6f2e4ad59 100644 --- a/tests/unit/swagger/schemaDetails.spec.ts +++ b/tests/unit/swagger/schemaDetails.spec.ts @@ -590,6 +590,28 @@ describe('Schema details generation', () => { expect(examples).to.deep.eq('test example response'); }); + + it('uses minus prefix token number value at @Example model', () => { + const metadata = new MetadataGenerator('./fixtures/controllers/exampleController.ts').Generate(); + const exampleSpec = new SpecGenerator2(metadata, getDefaultExtendedOptions()).GetSpec(); + const examples = exampleSpec.paths['/ExampleTest/ResponseExampleWithMinusOperatorPrefixValue']?.get?.responses?.[200]?.examples?.['application/json']; + + expect(examples).to.deep.eq({ + id: -1, + description: 'test doc des', + }); + }); + + it('uses plus prefix token number value at @Example model', () => { + const metadata = new MetadataGenerator('./fixtures/controllers/exampleController.ts').Generate(); + const exampleSpec = new SpecGenerator2(metadata, getDefaultExtendedOptions()).GetSpec(); + const examples = exampleSpec.paths['/ExampleTest/ResponseExampleWithPlusOperatorPrefixValue']?.get?.responses?.[200]?.examples?.['application/json']; + + expect(examples).to.deep.eq({ + id: 1, + description: 'test doc des', + }); + }); }); }); }); diff --git a/tests/unit/swagger/schemaDetails3.spec.ts b/tests/unit/swagger/schemaDetails3.spec.ts index 6478e972b..277493e6c 100644 --- a/tests/unit/swagger/schemaDetails3.spec.ts +++ b/tests/unit/swagger/schemaDetails3.spec.ts @@ -967,6 +967,36 @@ describe('Definition generation for OpenAPI 3.0.0', () => { }, }); }); + + it('uses minus prefix token number value at @Example model', () => { + const metadata = new MetadataGenerator('./fixtures/controllers/exampleController.ts').Generate(); + const exampleSpec = new SpecGenerator3(metadata, getDefaultExtendedOptions()).GetSpec(); + const examples = exampleSpec.paths['/ExampleTest/ResponseExampleWithMinusOperatorPrefixValue']?.get?.responses?.[200]?.content?.['application/json'].examples; + + expect(examples).to.deep.eq({ + 'Example 1': { + value: { + id: -1, + description: 'test doc des', + }, + }, + }); + }); + + it('uses plus prefix token number value at @Example model', () => { + const metadata = new MetadataGenerator('./fixtures/controllers/exampleController.ts').Generate(); + const exampleSpec = new SpecGenerator3(metadata, getDefaultExtendedOptions()).GetSpec(); + const examples = exampleSpec.paths['/ExampleTest/ResponseExampleWithPlusOperatorPrefixValue']?.get?.responses?.[200]?.content?.['application/json'].examples; + + expect(examples).to.deep.eq({ + 'Example 1': { + value: { + id: 1, + description: 'test doc des', + }, + }, + }); + }); }); describe('deprecation', () => { From 2d9d1e909c6bd85bd7dc8c8324dc96af0ea6c95f Mon Sep 17 00:00:00 2001 From: Clooooode Date: Thu, 6 Oct 2022 13:36:02 +0800 Subject: [PATCH 2/2] fix: lukeautry/tsoa#1289 Add minus(-) and plus(+) operator detection @ getInitializerValue when metadataGenerating. --- .../cli/src/metadataGeneration/initializer-value.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/cli/src/metadataGeneration/initializer-value.ts b/packages/cli/src/metadataGeneration/initializer-value.ts index 8e2c2e7b1..a0f04c8bf 100644 --- a/packages/cli/src/metadataGeneration/initializer-value.ts +++ b/packages/cli/src/metadataGeneration/initializer-value.ts @@ -21,6 +21,17 @@ export const getInitializerValue = (initializer?: ts.Expression | ts.ImportSpeci return true; case ts.SyntaxKind.FalseKeyword: return false; + case ts.SyntaxKind.PrefixUnaryExpression: { + const prefixUnary = initializer as ts.PrefixUnaryExpression; + switch (prefixUnary.operator) { + case ts.SyntaxKind.PlusToken: + return Number((prefixUnary.operand as ts.NumericLiteral).text); + case ts.SyntaxKind.MinusToken: + return Number(`-${(prefixUnary.operand as ts.NumericLiteral).text}`); + default: + throw new Error(`Unsupport prefix operator token: ${prefixUnary.operator}`); + } + } case ts.SyntaxKind.NumberKeyword: case ts.SyntaxKind.FirstLiteralToken: return Number((initializer as ts.NumericLiteral).text);