【问题标题】:Swagger UI seems to not handle optional keys with POST json bodySwagger UI 似乎无法处理带有 POST json 正文的可选键
【发布时间】:2023-11-16 00:05:02
【问题描述】:

包:

hapi-swagger: 9.0.1
joi: 13.0.2

上下文:

我将swagger-uihapi-swagger 生成的swagger.json 文件一起使用。

对于hapi-swagger,我将jsonEditor 选项设置为true。 这意味着我不会在单个文本区域中输入正文,而是直接使用 UI 生成的输入。

问题:

payload中只有"name"是必需的,如果我参考Joi文档,其他的默认是可选的。

其实Swagger-UI发送:

curl -X POST
--header 'Content-Type: application/json' 
--header 'Accept: application/json' 
-d '{
 "name":"fzef",

 "idsUser": [],

 "idsUsergroup":[]
}'

相反,我希望Swagger-UI 发送如下请求:

curl -X POST 
--header 'Content-Type: application/json' 
--header 'Accept: application/json' 
-d '{
 "name":"fzef",
 }'

招摇用户界面

Joi 架构

Joi.object().keys({

 request: Joi.object().keys({

     name: Joi.string().required(),

     idsUser: Joi.array().items(Joi.string()),

     idsUsergroup: Joi.array().items(Joi.string()),
   }),
  }),
 });

Swagger.json

...
"paths": {
  "/addCompany": {
    "post": {
      "operationId": "postAddcompany",

       "parameters": [{
          "type": "string",
          "default": "1",
          "pattern": "/^v[0-9]+$/",
          "name": "apiVersion",
          "in": "path",
          "required": true
        },
        {
          "in": "body",
          "name": "body",
          "schema": {
            "$ref": "#/definitions/Model 208"
           }
         }
       ],

      "tags": ["api", "CompanyCommandsAPIPart"],

      "responses": {
        "default": {
          "schema": {
            "type": "string"
          },
         "description": "Successful"
        },
      }
    }
  }
}

"definitions": {
  ...
  "Model 208": {
    "type": "object",

    "properties": {
      "name": {
        "type": "string"
      },

      "idsUser": {
         "$ref": "#/definitions/Model 13",
        "type": "array",
        "x-alternatives": [{
          "$ref": "#/x-alt-definitions/idsFunctionality",
          "type": "array"
        }, {
          "type": "string"
        }]
      },

      "idsUsergroup": {
         "$ref": "#/definitions/Model 13",
         "type": "array",
        "x-alternatives": [{
          "$ref": "#/x-alt-definitions/idsFunctionality",
          "type": "array"
        }, {
          "type": "string"
        }]
      },
    },

    "required": ["name"]
  },
  ...
}

我该怎么做才能得到这个请求正文?

我是否需要精确joi 方法,以便hapi-swagger 解析器将'optional' 之类的参数添加到swagger.json

我在 GET 方法的查询中发现了同样的问题,但没有找到解决方案:

https://github.com/swagger-api/swagger-editor/issues/684

【问题讨论】:

  • Swagger UI 发送的请求正文就是你输入的那个。你试过输入{"name": "fzef"}吗?
  • 道歉我忘了告诉我使用 hapi-swagger 的 jsonEditor 选项,我会编辑帖子
  • 能否在 Swagger UI 中添加请求正文配置的屏幕截图?
  • 刚刚在 Joi 架构之上完成

标签: node.js swagger swagger-ui joi hapi-swagger


【解决方案1】:

JSON Editor 组件提供“属性”和“编辑 JSON”按钮来自定义 JSON 有效负载,您可以在此处的组件演示中:http://jeremydorn.com/json-editor/。但是,Swagger UI 2.x(hapi-swagger 在撰写本文时使用的版本)使用 disable_properties: truedisable_edit_json: true 初始化 JSON 编辑器,以便隐藏这些按钮,并且 UI 不会将任何配置选项暴露给更改 JSON 编辑器选项。 GitHub 上的 hapi-editor 存储库中有一个未解决的问题:https://github.com/glennjones/hapi-swagger/issues/332

一种可能的解决方法是调整 Swagger UI 代码。假设您的 Swagger UI 的 index.html 使用未缩小的 swagger-ui.js,请在 <hapi-swagger>/public/swaggerui/swagger-ui.js 中找到以下行:

disable_properties:true,
disable_edit_json:true,

并将它们替换为:

disable_properties:false,
disable_edit_json:false,

现在 JSON 编辑器将具有“对象属性”按钮。单击此按钮可选择要在表单编辑器中显示并包含在请求正文中的属性。未选择的属性将不会在请求正文中发送。

【讨论】: