swagger - 在 NestJS 中,我可以在 Controller 级别添加什么装饰器以将 Authorization header 添加到我的 Swagger 文档?

标签 swagger nestjs openapi authorization-header

我正在使用 NestJS 7.6.11。我的 Controller 上有以下装饰器......

@Controller('private')
@ApiTags('MyObjects')
@ApiConsumes('application/json')
@ApiProduces('application/json')
@UseInterceptors(new JwtInterceptor())
export class MyController {
有没有我可以添加的装饰器会导致生成 Swagger(OpenAPI 3)文档,以便它表明我的 Controller 中的所有方法都需要有一个“授权” header ?
编辑:作为回应给出的答案,我添加了@ApiHeader 所以我的 Controller 和方法看起来像
@
Controller('myendpoint')
@ApiTags('MyObject')
@ApiConsumes('application/json')
@ApiProduces('application/json')
@ApiHeader({
  name: 'authorization',
  description: 'Auth token',
})
@UseInterceptors(new JwtInterceptor())
export class MyObjectController {

...
 @Get('/:id')
  @ApiOkResponse({
    description: 'OK',
    type: Content,
  })
  @ApiBadRequestResponse()
  @ApiInternalServerErrorResponse()
  @ApiOperation({
    summary: 'Get object by id',
    description: 'Get object by id',
    operationId: 'findObjectById',
  }) 
  findObjectById(@Req() req, @Param('id') id: string): Promise<MyObject> {
但是当生成 swagger 文档时,虽然我可以输入“授权”标题值,
enter image description here
当我单击“执行”时,它不会包含在我的 curl 中,它生成为
curl -X GET "http://localhost:8060/myendpoint/abcdef" -H  "accept: application/json"

最佳答案

@ApiHeader() , @ApiBasicAuth() , @ApiBearerAuth() , @ApiOAuth2() , @ApiSecurity() ,所有这些都可以在 this page 上找到.您的具体情况可能会有所不同,但其中之一应该可以解决问题。

关于swagger - 在 NestJS 中,我可以在 Controller 级别添加什么装饰器以将 Authorization header 添加到我的 Swagger 文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67952943/

相关文章:

node.js - 有没有办法在 typeORM 中自动创建数据库?

swagger - 根据 Insomnia 的说法,FastAPI 自动生成的 OpenAPI 文件无效

c# - 在 swagger C# .net 核心中使用 FormData 发送字符串数组?

php - API 平台 : Filtering Custom Data Provider

node.js - NestJS 加密 : What is the purpose of the promisify(scrypt)?

typescript - 类型错误 : Converting circular structure to JSON --> starting at object with constructor 'ClientRequest'

java - OpenAPI 3 支持 Jersey

java - 如何使用springdoc-openapi将Open API 3与Spring项目(而非Spring Boot)集成

wso2 - WSO2 API 管理什么时候开始支持 OpenAPI3.0 (Swagger 3.0)?

java - 如何让 REST 服务为 Java 8 (Local|Offset)DateTime 生成一个简单的字符串(使用 Swagger 时)?