NestJS swagger文档路径参数

2cmtqfgy  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(187)

我在使用NestJS的属性记录URL请求中的嵌入式参数时遇到了问题。
请求如下所示:
listId
处理具有swagger属性的POST的控制器和方法如下所示:

@Controller('api/todo-lists')
export class TodosController {
@Post('/:listId/todos')

 @ApiQuery({
  name: 'listId',
  description: "The ID of the list associated with this todo",
  required: true,
  type: String,
})
 async create(@AuthUser() user, @CurrentToDoList() todoList, @Body() todo: TodoCreateDto): 
<removed for brevity>
 }
}

这将创建以下Swagger文档:

paths:
  /api/todo-lists/{listId}/todos:
    post:
      operationId: TodosController_create
      parameters:
        - name: listId
          required: true
          in: query
          description: The ID of the list associated with this todo
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TodoCreateDto'

这将导致验证错误:

Semantic error at paths./api/todo-lists/{listId}/todos
Declared path parameter "listId" needs to be defined as a path parameter at either the path or operation level

我没有在对控制器本身的调用中使用listId,我有一个中间件,它抓取listID并示例化传递给每个控制器方法的列表。
我想在控制器级别添加listID文档,但不知道如何添加。

cl25kdpy

cl25kdpy1#

/:listId/todos这是URL参数的区段,不是查询参数。请使用@ApiParam()而非@ApiQuery()

相关问题