Skip to content

Commit 2c85ac9

Browse files
authored
feat: Support string variable in the first argument of @extension (#1677)
* feat: accepts identifiers * handle null? * undefined? * where is the null * refactor: attributeValue checks * fix: addressing tsoa-cli error * trying to understand lru-cache error - remove identifier * trying to understand lru-cache error - remove get value * trying to understand lru-cache error - remove all metadata changes * return to initial * handle null? * undefined? * where is the null * refactor: attributeValue checks * fix: addressing tsoa-cli error * trying to understand lru-cache error - remove identifier * trying to understand lru-cache error - remove get value * trying to understand lru-cache error - remove all metadata changes * return to initial * refactor: remove unneeded guard
1 parent 364288c commit 2c85ac9

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

packages/cli/src/metadataGeneration/extension.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ export function getExtensions(decorators: ts.Identifier[], metadataGenerator: Me
1212

1313
const [decoratorKeyArg, decoratorValueArg] = extensionDecorator.parent.arguments;
1414

15-
if (!ts.isStringLiteral(decoratorKeyArg)) {
15+
if (!ts.isStringLiteral(decoratorKeyArg) && !ts.isIdentifier(decoratorKeyArg)) {
1616
throw new Error('The first argument of @Extension must be a string');
1717
}
1818

19-
const attributeKey = decoratorKeyArg.text;
19+
const attributeKey = ts.isIdentifier(decoratorKeyArg) ? getInitializerValue(decoratorKeyArg, metadataGenerator.typeChecker) : decoratorKeyArg.text;
20+
21+
if (typeof attributeKey !== 'string') {
22+
throw new Error('The first argument of @Extension must be a string');
23+
}
2024

2125
if (!decoratorValueArg) {
2226
throw new Error(`Extension '${attributeKey}' must contain a value`);

tests/fixtures/controllers/methodController.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const TEST_SEC = {
2727
secondSec: [TEST_ENUM.ADMIN, TEST_ENUM.OWNER],
2828
};
2929

30+
const ATT_KEY9 = 'x-attKey9';
31+
3032
@Route('MethodTest')
3133
export class MethodController extends Controller {
3234
@Options('Options')
@@ -165,6 +167,7 @@ export class MethodController extends Controller {
165167
@Extension('x-attKey6', [{ y0: 'yt0', y1: 'yt1', y2: 123, y3: true, y4: null }, { y2: 'yt2' }])
166168
@Extension('x-attKey7', { test: ['testVal', 123, true, null] })
167169
@Extension('x-attKey8', { test: { testArray: ['testVal1', true, null, ['testVal2', 'testVal3', 123, true, null]] } })
170+
@Extension(ATT_KEY9, 'identifierAttValue')
168171
@Get('Extension')
169172
public async extension(): Promise<TestModel> {
170173
return new ModelService().getModel();

tests/unit/swagger/definitionsGeneration/metadata.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,13 @@ describe('Metadata generation', () => {
166166

167167
expect(method.responses.length).to.equal(5);
168168

169-
const badResponse = method.responses[1]
169+
const badResponse = method.responses[1];
170170
expect(badResponse.name).to.equal('400');
171171
expect(badResponse.description).to.equal('Bad Request');
172-
expect(badResponse.examples).to.deep.equal([{ status: 400, message: 'reason 1' }, { status: 400, message: 'reason 2' }]);
172+
expect(badResponse.examples).to.deep.equal([
173+
{ status: 400, message: 'reason 1' },
174+
{ status: 400, message: 'reason 2' },
175+
]);
173176

174177
const unauthResponse = method.responses[2];
175178
expect(unauthResponse.name).to.equal('401');
@@ -316,6 +319,7 @@ describe('Metadata generation', () => {
316319
{ key: 'x-attKey6', value: [{ y0: 'yt0', y1: 'yt1', y2: 123, y3: true, y4: null }, { y2: 'yt2' }] },
317320
{ key: 'x-attKey7', value: { test: ['testVal', 123, true, null] } },
318321
{ key: 'x-attKey8', value: { test: { testArray: ['testVal1', true, null, ['testVal2', 'testVal3', 123, true, null]] } } },
322+
{ key: 'x-attKey9', value: 'identifierAttValue' },
319323
];
320324

321325
expect(method.extensions).to.deep.equal(expectedExtensions);

0 commit comments

Comments
 (0)