nestjsx / nest-router

Router Module For Nestjs Framework 🚦 🚀
MIT License
615 stars 36 forks source link

Params in nested routes and swagger #130

Closed ghost closed 3 years ago

ghost commented 3 years ago

We are using https://github.com/nestjsx/nest-router#params-in-nested-routes, and it is working fine for the most part. We have an issue with https://github.com/nestjs/swagger, however. It does not recognize the nested route parameter. As you can see below, it is not declared in swagger.json.

swagger.json

Excerpt from /api-json:

    "/pets/{context}/cat/{name}": {
      "get": {
        "operationId": "CatController_getCatByName",
        "parameters": [
          {
            "name": "name",
            "required": true,
            "in": "path",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": ""
          }
        },
        "tags": [
          "Cat"
        ]
      }
    },

app.module.ts

import { RouterModule } from 'nest-router';
import { Module } from '@nestjs/common';
import { PetsModule } from './pets';

@Module({
  imports: [
    RouterModule.forRoutes([
      {
        path: '/pets/:context',
        module: PetsModule,
      },
    ]),
    PetsModule,
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

cat.controller.ts

import { Controller, Get, Param } from '@nestjs/common';
import { Cat } from './cat';
import { ApiTags } from '@nestjs/swagger';

@Controller('cat')
@ApiTags('Cat')
export class CatController {
  @Get(':name')
  getCatByName(@Param('name') name: string): Cat {
  }
}
shekohex commented 3 years ago

I guess Swagger integration got broken again! FYI, there is work done to get this module (RouterModule) in @nestjs/core itself. see https://github.com/nestjs/nest/pull/6035 it should be available on v8.

So, back to your issue here, I guess you can get around it for now, by adding @ApiParam() on top of your methods.

Also, you should report this issue in https://github.com/nestjs/swagger too! since it sounds like a parse issue, they don't take MODULE_PATH into account when resolving the whole path.

ghost commented 3 years ago

Well, that escalated quickly. 😃

It's great to hear that RouterModule is in @nestjs/core. As far as I can tell from https://github.com/nestjs/nest/pull/6035, params in nested routes are not included, however. So, I guess the next step is to create a feature request for that.

shekohex commented 3 years ago

that would be great feature!