lukeautry / tsoa

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

Build error: Property 'name' is missing #1582

Closed jeengbe closed 5 months ago

jeengbe commented 6 months ago

Sorting

Expected Behavior

should build successfully

Current Behavior

src/build/tsoa/routes.ts:37:21 - error TS2741: Property 'name' is missing in type '{ in: string; required: true; dataType: "object"; }' but required in type 'ParameterSchema'.

37                     undefined: {"in":"request","required":true,"dataType":"object"},
                       ~~~~~~~~~

  ../../node_modules/.pnpm/@tsoa+runtime@6.1.1/node_modules/@tsoa/runtime/dist/routeGeneration/tsoa-route.d.ts:50:9
    50         name: string;
               ~~~~
    'name' is declared here.

Found 1 error in src/build/tsoa/routes.ts:37

Possible Solution

Steps to Reproduce

import type { Request as KoaRequest } from 'koa';

  handleRequest(
    @Request() { req }: KoaRequest,
  ) {}  

Context (Environment)

Version of the library: 6.1.1

jackey8616 commented 6 months ago

@jeengbe Can you provide the version of 'koa' what you use plz.

Since that might cause by req field is not found in KoaRequest.

jeengbe commented 6 months ago

Sorry, I forgot to add this code snippet; this is what tsoa generates:

            const args: Record<string, TsoaRoute.ParameterSchema> = {
                    undefined: {"in":"request","required":true,"dataType":"object"},
            };

I assume this is because the parameter has no name, instead, is deconstructed directly. This issue was introduced with tsoa 6.1 and works fine on 6.0.

jackey8616 commented 6 months ago

@jeengbe I dig a little bit into the code.

This issue is not directly cause by recently update(^6.0.1), but discovered after implemented TemplateService to guard .hbs templates types.

The @Request is not support any deconstructing function parameter from the begining. (You can check the routes.ts generate by v6.0.1, you will find out that the key in args is still undefined)

TL;DR Fortunately, we do have a new decorator named @RequestProp to let you extract field from request, but currently we are not supporting any out-source type. So you have to do something like this:

import { IncomingMessage } from 'http';

handleRequest(
    @RequestProp('req') req: any,
) {
    // IncomingMessage is the type defined in koa's Request req filed.
    const koaReq: IncomingMessage = req as IncomingMessage;
    // other stuff you want
}
WoH commented 6 months ago

Did this ever work before? Or just not throw an error on build.

jackey8616 commented 6 months ago

Did this ever work before? Or just not throw an error on build. TL;DR never work in correct way, and not throw during build cause any type.

The deconstructing function parameter was work before with casting to any from hbs. And there is none of any type check even user setup a type in controller method.

But since we introduce TemplateService, everything comes from hbs should be include with type, thus the old coincidence is not work anymore.

shibukazu commented 6 months ago

Is this planned to be corrected in the future? Or will it remain as is?

jackey8616 commented 6 months ago

Is this planned to be corrected in the future? Or will it remain as is?

This is not an issue, you can consider use RequestProp to achieve similar functionality.

github-actions[bot] commented 5 months ago

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