Swagger UI 似乎无法处理带有 POST json 正文的可选键

Swagger UI seems to not handle optional keys with POST json body

套餐:

hapi-swagger: 9.0.1
joi: 13.0.2

上下文:

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

我将 hapi-swaggerjsonEditor 选项设置为 true。 这意味着我不会在单个文本区域中输入自己的正文,而是直接使用 UI.

生成的输入

问题:

有效负载中只有密钥 "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",
 }'

招摇UI

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 解析器向 swagger.json 添加像 'optional' 这样的参数?

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

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

JSON Editor component provides the "Properties" and "Edit JSON" buttons to customize the JSON payload, as you can in the component demo here: http://jeremydorn.com/json-editor/. However, Swagger UI 2.x (the version used by hapi-swagger at the time of writing) initializes the JSON Editor with disable_properties: true and disable_edit_json: true so that these buttons are hidden, and the UI does not expose any configuration options to change the JSON Editor options. There is an open issue in the hapi-editor repository on GitHub: 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 编辑器将具有 "Object Properties" 按钮。单击此按钮 select 属性将显示在表单编辑器中并包含在请求正文中。未selected 属性将不会在请求正文中发送。