[英]Swagger UI seems to not handle optional keys with POST json body
hapi-swagger: 9.0.1
joi: 13.0.2
我使用swagger-ui
和hapi-swagger
生成的swagger.json
文件。
我用hapi-swagger
將jsonEditor
選項設置為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",
}'
Joi.object().keys({
request: Joi.object().keys({
name: Joi.string().required(),
idsUser: Joi.array().items(Joi.string()),
idsUsergroup: Joi.array().items(Joi.string()),
}),
}),
});
...
"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方法的查詢存在同樣的問題,但沒有找到解決方案:
JSON編輯器組件提供“屬性”和“編輯JSON”按鈕以自定義JSON有效負載,您可以在此處的組件演示中查看: http : //jeremydorn.com/json-editor/ 。 但是,Swagger UI 2.x(hapi-swagger在編寫本文時使用的版本)使用disable_properties: true
初始化JSON編輯器disable_properties: true
和disable_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編輯器將具有“對象屬性”按鈕。 單擊此按鈕可選擇要在表單編輯器中顯示並包含在請求正文中的屬性。 未在請求正文中發送未選擇的屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.