samchon / nestia

NestJS Helper Libraries + TypeScript OpenAPI generator
https://nestia.io/
MIT License
1.85k stars 95 forks source link

Support regex path #143

Closed antyale closed 2 years ago

antyale commented 2 years ago

Bug Report

Summary

Validating path arguments does not take into account possible regex in the route parameters.

For example, the following endpoint:

  @Get(':id([0-9a-f]{24})')
  async getCall(
    @Req() req: Request,
    @Param('id') id: string,
  ): Promise<CallDTO> {

Will fail when generating the swagger json spec with Error on CallController.getCall(): binded arguments in the "path" between function's decorator and parameters' decorators are different (function: [id([0-9a-f]{24})], parameters: [id])

samchon commented 2 years ago

Wow, such regex on path parameter is really possible? I hadn't known at all.

Can you give me any guideline document about the regex path? I'll implement this feature until next wekk's Saturday.

It's okay that do not wait my implementation but sending PR by yourself ^^

antyale commented 2 years ago

Yes, it is something that Express allows, you can more details here (https://expressjs.com/en/guide/routing.html). According to the documentation: To have more control over the exact string that can be matched by a route parameter, you can append a regular expression in parentheses (()).

I actually made a quick fix locally in the ReflectAnalyzer.ts using map:

const funcPathArguments: string[] = StringUtil.betweens(
                    NodePath.join(controllerPath, metaPath)
                        .split("\\")
                        .join("/"),
                    ":",
                    "/",
                ).sort()
                  // Ignore regex when validating path arguments
                  .map(pathArgument => pathArgument.split('(')[0]);

That solved the validation issue but I found that the json spec is generated with the regex:

    "/call/{id}([0-9a-f]{24})": {
      "get": { .... }

I didn't have time to check that part yet, but I believe that this is not allowed in the openapi specification :sweat_smile:

samchon commented 2 years ago

@antyale

express is parsing the regex path using path-to-regexp.

Therefore, I will also utilize the library to support full path spec of the express.

Thanks for reporting.

samchon commented 2 years ago

@antyale New version nestia@3.0.11 supports the regex path.