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);