Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destructuring handler argument within function declaration causes tsoa to generate invalid code (Property 'name' is missing in type) #1654

Closed
2 of 4 tasks
alexng353 opened this issue Jul 29, 2024 · 3 comments
Labels

Comments

@alexng353
Copy link

alexng353 commented Jul 29, 2024

Sorting

  • I'm submitting a ...

    • bug report
    • feature request
    • support request
  • I confirm that I

    • used the search to make sure that a similar issue hasn't already been submit

Expected Behavior

export class ThingController extends Controller {
  async function handler(
    @Body() { email }: { email: string }
  ) { ... }
}

Using this code, tsoa should be able to generate properly.

Current Behavior

Currently, tsoa generates the following code.

Notice the undefined randomly in there. That is because (speculation) tsoa requires the variable that the argument to handler gets assigned to to have a name.

        app.post('/auth/v1/verify-email/verify-code',
            ...(fetchMiddlewares<RequestHandler>(VerifyEmailController)),
            ...(fetchMiddlewares<RequestHandler>(VerifyEmailController.prototype.verifyCodeRoute)),

            async function VerifyEmailController_verifyCodeRoute(request: ExRequest, response: ExResponse, next: any) {
            const args: Record<string, TsoaRoute.ParameterSchema> = {
                    undefined: {"in":"body","required":true,"dataType":"nestedObjectLiteral","nestedProperties":{"code":{"dataType":"string","required":true},"email":{"dataType":"string","required":true}}},
            };

            // WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa

            let validatedArgs: any[] = [];
            try {
                validatedArgs = templateService.getValidatedArgs({ args, request, response });

                const controller = new VerifyEmailController();

              await templateService.apiHandler({
                methodName: 'verifyCodeRoute',
                controller,
                response,
                next,
                validatedArgs,
                successStatus: undefined,
              });
            } catch (err) {
                return next(err);
            }
        });

Possible Solution

Steps to Reproduce

  1. Use the provided code (above) and try it yourself.

Context (Environment)

Version of the library: ^6.4.0
Version of NodeJS: v20.15.0

  • Confirm you were using yarn not npm: [x]

Detailed Description

It will default to body instead of undefined, depending on which @decorator is being used.

Breaking change?

@alexng353
Copy link
Author

The quick fix, for anyone who comes across this issue, is to use a named variable instead of object property destructuring.

@alexng353 alexng353 changed the title Destructuring handler argument within function declaration causes tsoa to generate invalid code Destructuring handler argument within function declaration causes tsoa to generate invalid code (Property 'name' is missing in type) Jul 29, 2024
@alexng353
Copy link
Author

I believe this is the same issue as found in #1582 . Another solution instead of changing the code would be to make it extremely obvious in the documentation that it is REQUIRED to use named arguments instead of object destructuring.

Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant