我正在使用 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 文档时,虽然我可以输入“授权”标题值,当我单击“执行”时,它不会包含在我的 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/