我正在使用nestjs swagger 模块并想创建我的 API 文档。对于依赖请求正文的端点,我可以将 DTO 类分配给文档,例如
@ApiBody({ type: CreateUserDTO })
Run Code Online (Sandbox Code Playgroud)
一些端点还依赖于请求参数或查询。对于参数,我会做类似的事情
@ApiParam({ type: GetUserByIdDTO })
Run Code Online (Sandbox Code Playgroud)
(我知道这是一个不好的例子,因为用户 id 不需要 DTO,但假设您想使用类验证器使用 DTO 类验证您的参数)
但我收到了这个错误
类型参数 '{ type: typeof GetUserByIdDTO; }' 不可分配给类型为 'ApiParamOptions' 的参数。类型 '{ type: typeof GetUserByIdDTO; 中缺少属性 'name' }' 但在'ApiParamMetadata'类型中是必需的。
对于查询,我会做类似的事情
@ApiQuery({ type: GetUsersDTO })
Run Code Online (Sandbox Code Playgroud)
并得到这个错误
类型参数 '{ type: typeof GetUsersDTO; }' 不能分配给'ApiQueryOptions' 类型的参数。类型 '{ type: typeof GetUsersDTO; 中缺少属性 'name' }' 但在'ApiQueryMetadata'类型中是必需的。
所以APIBody装饰器似乎工作正常,但我该如何修复我的APIParam和APIQuery装饰器?
@ApiQuery并@ApiParam在使用命名参数/查询时需要,例如@Query('pageSize')o @Param('id')。在这种情况下,NestJS Swagger 模块应该直接从指定的 DTO 对象中提取信息,例如:
async findElements(@Query() query: ElementsQueryDto) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
需要注意的重要一点是,Dtos 应该是类,而不是接口。
| 归档时间: |
|
| 查看次数: |
3423 次 |
| 最近记录: |