Skip to content

Commit

Permalink
feat: Support string variable in the first argument of @extension (#1677
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
chappelo committed Sep 26, 2024
1 parent 364288c commit 2c85ac9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/cli/src/metadataGeneration/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/controllers/methodController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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<TestModel> {
return new ModelService().getModel();
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/swagger/definitionsGeneration/metadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2c85ac9

Please sign in to comment.