簡體   English   中英

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生成的輸入。

問題 :

如果我引用Joi文檔,則有效負載中只需要密鑰"name" ,其他缺省值是可選的。

實際上, 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",
 }'

Swagger UI

在此輸入圖像描述

Joi Schema

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'這樣的swagger.json嗎?

我發現GET方法的查詢存在同樣的問題,但沒有找到解決方案:

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

JSON編輯器組件提供“屬性”和“編輯JSON”按鈕以自定義JSON有效負載,您可以在此處的組件演示中查看: http//jeremydorn.com/json-editor/ 但是,Swagger UI 2.x(hapi-swagger在編寫本文時使用的版本)使用disable_properties: true初始化JSON編輯器disable_properties: truedisable_edit_json: true以便隱藏這些按鈕,並且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編輯器將具有“對象屬性”按鈕。 單擊此按鈕可選擇要在表單編輯器中顯示並包含在請求正文中的屬性。 未在請求正文中發送未選擇的屬性。

Swagger UI  -  JSON編輯器 - 不包括可選屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM