如何在 swagger 文档中将授权承载令牌作为标头参数传递

问题描述 投票:0回答:1

我正在尝试通过 swagger 文档中的标头传递承载授权令牌,但我没有收到标头,

当我记录请求时,这不包括标头

/**
 * @swagger
 * components:
 *   securitySchemes:
 *     BearerAuth:
 *       type: http
 *       scheme: bearer
 *       bearerFormat: JWT

 * /api/v1/mediator/verify/phone/otp:
 *   post:
 *     summary: Verify Phone OTP
 *     tags: [Users]
 *     security:
 *       - BearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               phone_number:
 *                 type: string
 *                 description: The phone number to verify OTP.
 *               code:
 *                 type: string
 *                 description: The OTP code to verify.
 *     responses:
 *       '200':
 *         description: Phone OTP verification successful.
 *       '400':
 *         description: Bad request - validation failed or phone_number/code not provided.
 *       '401':
 *         description: Unauthorized - Missing or invalid token. This endpoint requires a bearer token for authorization.
 *       '500':
 *         description: Internal server error.
 */

这不会弹出任何必填字段让我传递授权令牌

/**
 * @swagger
 * /api/v1/mediator/verify/email/otp:
 *   post:
 *     summary: Verify Email OTP
 *     tags: [Users]
 *     security:
 *       - BearerAuth: []
 *     parameters:
 *       - in: header
 *         name: Authorization
 *         required: true
 *         schema:
 *           type: string
 *           format: BearerToken
 *           description: Bearer token for authorization.
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               email:
 *                 type: string
 *                 description: The phone number to verify OTP.
 *               code:
 *                 type: string
 *                 description: The OTP code to verify.
 *     responses:
 *       200:
 *         description: Phone OTP verification successful.
 *       400:
 *         description: Bad request - validation failed or email/code not provided.
 *       401:
 *         description: Unauthorized - invalid or missing token.
 *       500:
 *         description: Internal server error.
 */

我在这里做错了什么,我使用的是 openapi 3.0.0,当我使用邮递员尝试这个时,它工作正常,但在 swagger 上不起作用,我能够从标头接收任何参数。

javascript node.js swagger openapi
1个回答
0
投票

这是更正后的版本:

/**
 * @swagger
 * components:
 *   securitySchemes:
 *     BearerAuth:
 *       type: http
 *       scheme: bearer
 *       bearerFormat: JWT
 *
 * /api/v1/mediator/verify/phone/otp:
 *   post:
 *     summary: Verify Phone OTP
 *     tags: [Users]
 *     security:
 *       - BearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               phone_number:
 *                 type: string
 *                 description: The phone number to verify OTP.
 *               code:
 *                 type: string
 *                 description: The OTP code to verify.
 *     responses:
 *       '200':
 *         description: Phone OTP verification successful.
 *       '400':
 *         description: Bad request - validation failed or phone_number/code not provided.
 *       '401':
 *         description: Unauthorized - Missing or invalid token. This endpoint requires a bearer token for authorization.
 *       '500':
 *         description: Internal server error.
 */
© www.soinside.com 2019 - 2024. All rights reserved.