lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.42k stars 489 forks source link

Support string variable in the first argument of @Extension #1520

Open tszshingt opened 8 months ago

tszshingt commented 8 months ago

If I use a string variable in the first argument of @Extension:

const MY_EXTENSION = "x-extension";
@Extension(MY_EXTENSION, "first extension")

it throws the following error:

Error: The first argument of @Extension must be a string

I have tried the followings but none of them worked:

const MY_EXTENSION = "x-extension" as const;
@Extension(MY_EXTENSION, "first extension")

const MY_EXTENSION : "x-extension" = "x-extension";
@Extension(MY_EXTENSION, "first extension")

Currently, the only way to set the key is to use a string literal:

@Extension("x-extension", "first extension")

I am going to be using the key x-extension in many API endpoints, so it would be nice to be able to declare a constant as the key to reduce errors.

Is there a workaround to pass in string variable as the first argument of @Extension?

Sorting

Expected Behavior

We should be able to pass in a string variable like in other decorators.

Current Behavior

It throws an error when I pass in a string variable as the first argument of @Extension.

Possible Solution

The error is thrown from this line of code: https://github.com/lukeautry/tsoa/blob/8b30ec76a2ea460553a26a511317e918bc503da6/packages/cli/src/metadataGeneration/extension.ts#L16C1-L16C1

It is currently checking the argument using ts.isStringLiteral(). Could we just simply check if it is a string instead of String Literal?

Steps to Reproduce

See description above.

      1. 4.

Context (Environment)

Version of the library: 3.14.1 Version of NodeJS: 16.20.2

Detailed Description

See description above.

Breaking change?

Not sure. String is a superset of String Literal, so it should not break existing functionalities, but not sure if String Literal was required for any reason.

github-actions[bot] commented 8 months ago

Hello there tszshingt 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀