From 88d13c06568c34fb267a4fcddb4a522e798c089f Mon Sep 17 00:00:00 2001 From: Josh Chappelow Date: Sun, 15 Sep 2024 15:02:57 +0900 Subject: [PATCH 01/10] feat: accepts identifiers --- packages/cli/src/metadataGeneration/extension.ts | 8 ++++++-- tests/fixtures/controllers/methodController.ts | 3 +++ tests/unit/swagger/definitionsGeneration/metadata.spec.ts | 8 ++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/metadataGeneration/extension.ts b/packages/cli/src/metadataGeneration/extension.ts index 08ee2ffc4..69c43be27 100644 --- a/packages/cli/src/metadataGeneration/extension.ts +++ b/packages/cli/src/metadataGeneration/extension.ts @@ -12,11 +12,15 @@ export function getExtensions(decorators: ts.Identifier[], metadataGenerator: Me const [decoratorKeyArg, decoratorValueArg] = extensionDecorator.parent.arguments; - if (!ts.isStringLiteral(decoratorKeyArg)) { + if (!ts.isStringLiteral(decoratorKeyArg) && !ts.isIdentifier(decoratorKeyArg)) { throw new Error('The first argument of @Extension must be a string'); } - const attributeKey = decoratorKeyArg.text; + const attributeKey = ts.isIdentifier(decoratorKeyArg) ? getInitializerValue(decoratorKeyArg, metadataGenerator.typeChecker) : decoratorKeyArg.text; + + if (typeof attributeKey !== 'string') { + throw new Error('The first argument of @Extension must be a string'); + } if (!decoratorValueArg) { throw new Error(`Extension '${attributeKey}' must contain a value`); diff --git a/tests/fixtures/controllers/methodController.ts b/tests/fixtures/controllers/methodController.ts index 2989c06dd..5c668d275 100644 --- a/tests/fixtures/controllers/methodController.ts +++ b/tests/fixtures/controllers/methodController.ts @@ -27,6 +27,8 @@ const TEST_SEC = { secondSec: [TEST_ENUM.ADMIN, TEST_ENUM.OWNER], }; +const ATT_KEY9 = 'x-attKey9'; + @Route('MethodTest') export class MethodController extends Controller { @Options('Options') @@ -165,6 +167,7 @@ export class MethodController extends Controller { @Extension('x-attKey6', [{ y0: 'yt0', y1: 'yt1', y2: 123, y3: true, y4: null }, { y2: 'yt2' }]) @Extension('x-attKey7', { test: ['testVal', 123, true, null] }) @Extension('x-attKey8', { test: { testArray: ['testVal1', true, null, ['testVal2', 'testVal3', 123, true, null]] } }) + @Extension(ATT_KEY9, 'identifierAttValue') @Get('Extension') public async extension(): Promise { return new ModelService().getModel(); diff --git a/tests/unit/swagger/definitionsGeneration/metadata.spec.ts b/tests/unit/swagger/definitionsGeneration/metadata.spec.ts index 85b75aa0b..4cc5097d6 100644 --- a/tests/unit/swagger/definitionsGeneration/metadata.spec.ts +++ b/tests/unit/swagger/definitionsGeneration/metadata.spec.ts @@ -166,10 +166,13 @@ describe('Metadata generation', () => { expect(method.responses.length).to.equal(5); - const badResponse = method.responses[1] + const badResponse = method.responses[1]; expect(badResponse.name).to.equal('400'); expect(badResponse.description).to.equal('Bad Request'); - expect(badResponse.examples).to.deep.equal([{ status: 400, message: 'reason 1' }, { status: 400, message: 'reason 2' }]); + expect(badResponse.examples).to.deep.equal([ + { status: 400, message: 'reason 1' }, + { status: 400, message: 'reason 2' }, + ]); const unauthResponse = method.responses[2]; expect(unauthResponse.name).to.equal('401'); @@ -316,6 +319,7 @@ describe('Metadata generation', () => { { key: 'x-attKey6', value: [{ y0: 'yt0', y1: 'yt1', y2: 123, y3: true, y4: null }, { y2: 'yt2' }] }, { key: 'x-attKey7', value: { test: ['testVal', 123, true, null] } }, { key: 'x-attKey8', value: { test: { testArray: ['testVal1', true, null, ['testVal2', 'testVal3', 123, true, null]] } } }, + { key: 'x-attKey9', value: 'identifierAttValue' }, ]; expect(method.extensions).to.deep.equal(expectedExtensions); From baed052ef8c44f1cba28aa253723d03e5176561d Mon Sep 17 00:00:00 2001 From: Josh Chappelow Date: Sun, 15 Sep 2024 15:38:37 +0900 Subject: [PATCH 02/10] handle null? --- packages/cli/src/metadataGeneration/extension.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/metadataGeneration/extension.ts b/packages/cli/src/metadataGeneration/extension.ts index 69c43be27..4ba1e45da 100644 --- a/packages/cli/src/metadataGeneration/extension.ts +++ b/packages/cli/src/metadataGeneration/extension.ts @@ -18,8 +18,8 @@ export function getExtensions(decorators: ts.Identifier[], metadataGenerator: Me const attributeKey = ts.isIdentifier(decoratorKeyArg) ? getInitializerValue(decoratorKeyArg, metadataGenerator.typeChecker) : decoratorKeyArg.text; - if (typeof attributeKey !== 'string') { - throw new Error('The first argument of @Extension must be a string'); + if (typeof attributeKey !== 'string' || attributeKey == null) { + throw new Error('The first argument of @Extension must a string'); } if (!decoratorValueArg) { From 8bffdab3e7f82e15e983f6754c8c773bcd0e5088 Mon Sep 17 00:00:00 2001 From: Josh Chappelow Date: Sun, 15 Sep 2024 15:56:05 +0900 Subject: [PATCH 03/10] undefined? --- packages/cli/src/metadataGeneration/extension.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/cli/src/metadataGeneration/extension.ts b/packages/cli/src/metadataGeneration/extension.ts index 4ba1e45da..c22700e52 100644 --- a/packages/cli/src/metadataGeneration/extension.ts +++ b/packages/cli/src/metadataGeneration/extension.ts @@ -29,6 +29,9 @@ export function getExtensions(decorators: ts.Identifier[], metadataGenerator: Me assertValidExtensionKey(attributeKey); const attributeValue = getInitializerValue(decoratorValueArg, metadataGenerator.typeChecker); + if (attributeValue === undefined) { + throw new Error(`'${attributeKey}' is undefined`); + } if (!isNonUndefinedInitializerValue(attributeValue)) { throw new Error(`Extension '${attributeKey}' cannot have an undefined initializer value`); } From a614ede2a30b027fe83e107b33cbac14ecef64b8 Mon Sep 17 00:00:00 2001 From: Josh Chappelow Date: Sun, 15 Sep 2024 16:36:37 +0900 Subject: [PATCH 04/10] where is the null --- packages/cli/src/metadataGeneration/extension.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/metadataGeneration/extension.ts b/packages/cli/src/metadataGeneration/extension.ts index c22700e52..d30444260 100644 --- a/packages/cli/src/metadataGeneration/extension.ts +++ b/packages/cli/src/metadataGeneration/extension.ts @@ -29,8 +29,8 @@ export function getExtensions(decorators: ts.Identifier[], metadataGenerator: Me assertValidExtensionKey(attributeKey); const attributeValue = getInitializerValue(decoratorValueArg, metadataGenerator.typeChecker); - if (attributeValue === undefined) { - throw new Error(`'${attributeKey}' is undefined`); + if (attributeValue == null) { + throw new Error(`The value for extension '${attributeKey}' cannot be null or undefined.`); } if (!isNonUndefinedInitializerValue(attributeValue)) { throw new Error(`Extension '${attributeKey}' cannot have an undefined initializer value`); From 5bdd501c376d0556e65b81a2beff32cb9cb109d8 Mon Sep 17 00:00:00 2001 From: Josh Chappelow Date: Sun, 15 Sep 2024 17:12:39 +0900 Subject: [PATCH 05/10] refactor: attributeValue checks --- packages/cli/src/metadataGeneration/extension.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/cli/src/metadataGeneration/extension.ts b/packages/cli/src/metadataGeneration/extension.ts index d30444260..7229952f9 100644 --- a/packages/cli/src/metadataGeneration/extension.ts +++ b/packages/cli/src/metadataGeneration/extension.ts @@ -32,6 +32,9 @@ export function getExtensions(decorators: ts.Identifier[], metadataGenerator: Me if (attributeValue == null) { throw new Error(`The value for extension '${attributeKey}' cannot be null or undefined.`); } + if (typeof attributeValue !== 'string' && typeof attributeValue !== 'number' && typeof attributeValue !== 'boolean') { + throw new Error(`Extension '${attributeKey}' has an invalid value type: ${typeof attributeValue}`); + } if (!isNonUndefinedInitializerValue(attributeValue)) { throw new Error(`Extension '${attributeKey}' cannot have an undefined initializer value`); } From d8b25edc1f84950aec32b32a993bee8fd5385052 Mon Sep 17 00:00:00 2001 From: Josh Chappelow Date: Sun, 15 Sep 2024 22:01:13 +0900 Subject: [PATCH 06/10] fix: addressing tsoa-cli error --- packages/cli/src/metadataGeneration/extension.ts | 8 +------- packages/cli/src/swagger/specGenerator3.ts | 1 + 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/metadataGeneration/extension.ts b/packages/cli/src/metadataGeneration/extension.ts index 7229952f9..e0523029e 100644 --- a/packages/cli/src/metadataGeneration/extension.ts +++ b/packages/cli/src/metadataGeneration/extension.ts @@ -18,7 +18,7 @@ export function getExtensions(decorators: ts.Identifier[], metadataGenerator: Me const attributeKey = ts.isIdentifier(decoratorKeyArg) ? getInitializerValue(decoratorKeyArg, metadataGenerator.typeChecker) : decoratorKeyArg.text; - if (typeof attributeKey !== 'string' || attributeKey == null) { + if (typeof attributeKey !== 'string') { throw new Error('The first argument of @Extension must a string'); } @@ -29,12 +29,6 @@ export function getExtensions(decorators: ts.Identifier[], metadataGenerator: Me assertValidExtensionKey(attributeKey); const attributeValue = getInitializerValue(decoratorValueArg, metadataGenerator.typeChecker); - if (attributeValue == null) { - throw new Error(`The value for extension '${attributeKey}' cannot be null or undefined.`); - } - if (typeof attributeValue !== 'string' && typeof attributeValue !== 'number' && typeof attributeValue !== 'boolean') { - throw new Error(`Extension '${attributeKey}' has an invalid value type: ${typeof attributeValue}`); - } if (!isNonUndefinedInitializerValue(attributeValue)) { throw new Error(`Extension '${attributeKey}' cannot have an undefined initializer value`); } diff --git a/packages/cli/src/swagger/specGenerator3.ts b/packages/cli/src/swagger/specGenerator3.ts index cc17ee02d..1d7c4b0f1 100644 --- a/packages/cli/src/swagger/specGenerator3.ts +++ b/packages/cli/src/swagger/specGenerator3.ts @@ -696,6 +696,7 @@ export class SpecGenerator3 extends SpecGenerator { if (types.size === 1) { const type = types.values().next().value; + if (!type) throw new Error('Enum must be defined'); const nullable = enumType.enums.includes(null) ? true : false; return { ...(title && { title }), type, enum: enumType.enums.map(member => getValue(type, member)), nullable }; } else { From d3c4da19e4471266b3ccb83873bf4c64ccf14b71 Mon Sep 17 00:00:00 2001 From: Josh Chappelow Date: Sun, 15 Sep 2024 22:40:28 +0900 Subject: [PATCH 07/10] trying to understand lru-cache error - remove identifier --- packages/cli/src/metadataGeneration/extension.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/metadataGeneration/extension.ts b/packages/cli/src/metadataGeneration/extension.ts index e0523029e..c27286e3a 100644 --- a/packages/cli/src/metadataGeneration/extension.ts +++ b/packages/cli/src/metadataGeneration/extension.ts @@ -12,7 +12,7 @@ export function getExtensions(decorators: ts.Identifier[], metadataGenerator: Me const [decoratorKeyArg, decoratorValueArg] = extensionDecorator.parent.arguments; - if (!ts.isStringLiteral(decoratorKeyArg) && !ts.isIdentifier(decoratorKeyArg)) { + if (!ts.isStringLiteral(decoratorKeyArg)) { throw new Error('The first argument of @Extension must be a string'); } From 985cb054b758c180a9fa20c47db3d39dd91ce1b3 Mon Sep 17 00:00:00 2001 From: Josh Chappelow Date: Sun, 15 Sep 2024 22:42:40 +0900 Subject: [PATCH 08/10] trying to understand lru-cache error - remove get value --- packages/cli/src/metadataGeneration/extension.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/metadataGeneration/extension.ts b/packages/cli/src/metadataGeneration/extension.ts index c27286e3a..46c4f9b13 100644 --- a/packages/cli/src/metadataGeneration/extension.ts +++ b/packages/cli/src/metadataGeneration/extension.ts @@ -12,11 +12,11 @@ export function getExtensions(decorators: ts.Identifier[], metadataGenerator: Me const [decoratorKeyArg, decoratorValueArg] = extensionDecorator.parent.arguments; - if (!ts.isStringLiteral(decoratorKeyArg)) { + if (!ts.isStringLiteral(decoratorKeyArg) && !ts.isIdentifier(decoratorKeyArg)) { throw new Error('The first argument of @Extension must be a string'); } - const attributeKey = ts.isIdentifier(decoratorKeyArg) ? getInitializerValue(decoratorKeyArg, metadataGenerator.typeChecker) : decoratorKeyArg.text; + const attributeKey = decoratorKeyArg.text; if (typeof attributeKey !== 'string') { throw new Error('The first argument of @Extension must a string'); From c7dfd58c64c9efe616a1daa37946b72dd358c7ee Mon Sep 17 00:00:00 2001 From: Josh Chappelow Date: Sun, 15 Sep 2024 22:46:09 +0900 Subject: [PATCH 09/10] trying to understand lru-cache error - remove all metadata changes --- packages/cli/src/metadataGeneration/extension.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/cli/src/metadataGeneration/extension.ts b/packages/cli/src/metadataGeneration/extension.ts index 46c4f9b13..08ee2ffc4 100644 --- a/packages/cli/src/metadataGeneration/extension.ts +++ b/packages/cli/src/metadataGeneration/extension.ts @@ -12,16 +12,12 @@ export function getExtensions(decorators: ts.Identifier[], metadataGenerator: Me const [decoratorKeyArg, decoratorValueArg] = extensionDecorator.parent.arguments; - if (!ts.isStringLiteral(decoratorKeyArg) && !ts.isIdentifier(decoratorKeyArg)) { + if (!ts.isStringLiteral(decoratorKeyArg)) { throw new Error('The first argument of @Extension must be a string'); } const attributeKey = decoratorKeyArg.text; - if (typeof attributeKey !== 'string') { - throw new Error('The first argument of @Extension must a string'); - } - if (!decoratorValueArg) { throw new Error(`Extension '${attributeKey}' must contain a value`); } From 2b84fb50502047cd3ba81784b077e5beea87ba91 Mon Sep 17 00:00:00 2001 From: Josh Chappelow Date: Sun, 15 Sep 2024 22:58:32 +0900 Subject: [PATCH 10/10] return to initial --- packages/cli/src/metadataGeneration/extension.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/metadataGeneration/extension.ts b/packages/cli/src/metadataGeneration/extension.ts index 08ee2ffc4..69c43be27 100644 --- a/packages/cli/src/metadataGeneration/extension.ts +++ b/packages/cli/src/metadataGeneration/extension.ts @@ -12,11 +12,15 @@ export function getExtensions(decorators: ts.Identifier[], metadataGenerator: Me const [decoratorKeyArg, decoratorValueArg] = extensionDecorator.parent.arguments; - if (!ts.isStringLiteral(decoratorKeyArg)) { + if (!ts.isStringLiteral(decoratorKeyArg) && !ts.isIdentifier(decoratorKeyArg)) { throw new Error('The first argument of @Extension must be a string'); } - const attributeKey = decoratorKeyArg.text; + const attributeKey = ts.isIdentifier(decoratorKeyArg) ? getInitializerValue(decoratorKeyArg, metadataGenerator.typeChecker) : decoratorKeyArg.text; + + if (typeof attributeKey !== 'string') { + throw new Error('The first argument of @Extension must be a string'); + } if (!decoratorValueArg) { throw new Error(`Extension '${attributeKey}' must contain a value`);