Swagger:如何制作"执行";按钮禁用,如果需要参数haven'我没有接到通知



我正在使用Swagger UI Express为一个非常简单的节点API创建文档。它更像是一个";UI/UX";问题,但困扰着我。

我有一些路由,其中需要一个路径参数(例如客户端id(,我已经用这种方式进行了记录。因此,Swagger UI上的参数被标记为必需。然而,用户可以点击";执行";按钮,而不填写所需参数。如果页面返回错误并让用户填写所需的参数并重试,这不会是一个大问题,但事实并非如此。单击按钮后,将显示一个加载gif并继续加载。。。即使用户点击";取消";按钮,它不会停止加载。

在控制台上,出现以下错误:

react_devtools_backend.js:4026 Error: Required parameter codCliente is not provided
    at index.js:233:13
    at Array.forEach (<anonymous>)
    at Object.Bn [as buildRequest] (index.js:200:22)
    at actions.js:452:24
    at Object.dispatch (utils.js:177:16)
    at dispatch (<anonymous>:3665:80)
    at redux.js:546:12
    at wrap-actions.js:33:10
    at Object.r (system.js:175:26)
    at Object.executeRequest (system.js:487:14)

这是发生问题的屏幕截图。我们可以根据需要查看参数,但UI允许您单击";试试看"然后点击";执行";。

以下是路由器和控制器代码的示例:

const getByCod = async (req: Request, res: Response): Promise<Response> => {
  const { codCliente } = req.params;
  const cliente = await clientesService.getByCod(parseInt(codCliente, 10));
  return res.status(StatusCodes.OK).json({ data: cliente });
};
clientesRouter.get(
  'clientes/:codCliente',
  authenticationMiddleware,
  authorizationMiddleware,
  getByCod,
);

这里是我在swagger.json:中的招摇路径定义

"/clientes/{codCliente}": {
      "get": {
        "summary": '...',
        "description": '...',
        "tags": ["Clientes"],
        "security": [...],
        "parameters": [
          { 
            "in": "path",
            "name": "codCliente",
            "description": "Código do cliente",
            "required": true,
            "type": "integer"
          }
        ],
        "responses": {
          [...]         
        }
      }
    },

在OpenAPI 3.x中,参数type必须封装到schema关键字中。缺少schema是Swagger UI出现问题的原因。

        "parameters": [
          { 
            "in": "path",
            "name": "codCliente",
            "description": "Código do cliente",
            "required": true,
            "schema": {            <-----------
              "type": "integer"
            }
          }
        ],

没有schema的语法(如原始示例中所示(在规范的早期版本OpenAPI 2.0(swagger: '2.0'(中使用。

您可以使用检查OpenAPI定义中的语法错误https://editor.swagger.io或其他验证器。

最新更新