在 swaggerUI 中使 requestBody 字段可选
make requestBody fields optional in swaggerUI
为我的 flask python 项目使用以下版本:
连接==2.3.0
swagger-ui-bundle==0.0.5
美洲国家组织 3.0
我想让所有的 requestBody 字段都是可选的,现在我已经定义了 json 如下所示,我假设如果你不在 requestBody 中提供 required 字段,那么它应该被视为 false,如果我没有为这些字段提供 swagger UI 中的任何值,它不应该使用带有 -d 选项的那些字段生成 curl 请求。
"openapi": "3.0.0",
"info": {
"description": "Dev",
"version": "1.0.0",
"title": "DEV-API",
"contact": {
"email": "dev@email.com"
},
"license": {
"name": "Dev",
"url": "https://opensource.org/licenses/MIT"
}
},
"servers": [
{
"url": "http://xx.yy.zz.a:8080"
}
],
"tags": [
{
"name": "Custom Event Post Request",
"description": "Example API for posting custom events"
}
],
"paths": {
"/api/v1/calls/{id}/{event-name}": {
"post": {
"tags": [
"Post Event"
],
"summary": "Post Custom Event to a call",
"operationId": "post_call_custom_event_data",
"parameters": [
{
"name": "id",
"in": "path",
"schema": {
"type": "string"
},
"required": true
},
{
"name": "event-name",
"in": "path",
"schema": {
"type": "string"
},
"required": true
}
],
"requestBody": {
"content": {
"application/x-www-form-urlencoded":{
"schema": {
"type": "object",
"properties": {
"event1": {
"type":"boolean"
},
"event2":{
"type": "string"
},
"event3": {
"type":"string"
}
}
},
"encoding": {
"event2": {
"allowReserved": true
},
"event3": {
"allowReserved": true
}
}
}
}
},
"responses": {
"200": {
"description": "ok"
},
"400": {
"description": "Failed. Bad Post Data"
}
}
}
}
}
但它确实没有生成如下值:
curl -X POST "http://xx.yy.zz.a:8080/api/v1/calls/5454/custom-event" -H "accept: /" -H "Content-Type: application/x-www-form-urlencoded" -d "event1=&event2=&document="
我不确定如何使 requestBody fields/elements 在 swagger 中显示为可选 UI
您的 API 定义是正确的。这是 Swagger UI 的一个限制,它总是发送所有表单字段,包括具有空值的可选字段。此处跟踪此问题:
为我的 flask python 项目使用以下版本:
连接==2.3.0 swagger-ui-bundle==0.0.5 美洲国家组织 3.0
我想让所有的 requestBody 字段都是可选的,现在我已经定义了 json 如下所示,我假设如果你不在 requestBody 中提供 required 字段,那么它应该被视为 false,如果我没有为这些字段提供 swagger UI 中的任何值,它不应该使用带有 -d 选项的那些字段生成 curl 请求。
"openapi": "3.0.0",
"info": {
"description": "Dev",
"version": "1.0.0",
"title": "DEV-API",
"contact": {
"email": "dev@email.com"
},
"license": {
"name": "Dev",
"url": "https://opensource.org/licenses/MIT"
}
},
"servers": [
{
"url": "http://xx.yy.zz.a:8080"
}
],
"tags": [
{
"name": "Custom Event Post Request",
"description": "Example API for posting custom events"
}
],
"paths": {
"/api/v1/calls/{id}/{event-name}": {
"post": {
"tags": [
"Post Event"
],
"summary": "Post Custom Event to a call",
"operationId": "post_call_custom_event_data",
"parameters": [
{
"name": "id",
"in": "path",
"schema": {
"type": "string"
},
"required": true
},
{
"name": "event-name",
"in": "path",
"schema": {
"type": "string"
},
"required": true
}
],
"requestBody": {
"content": {
"application/x-www-form-urlencoded":{
"schema": {
"type": "object",
"properties": {
"event1": {
"type":"boolean"
},
"event2":{
"type": "string"
},
"event3": {
"type":"string"
}
}
},
"encoding": {
"event2": {
"allowReserved": true
},
"event3": {
"allowReserved": true
}
}
}
}
},
"responses": {
"200": {
"description": "ok"
},
"400": {
"description": "Failed. Bad Post Data"
}
}
}
}
}
但它确实没有生成如下值:
curl -X POST "http://xx.yy.zz.a:8080/api/v1/calls/5454/custom-event" -H "accept: /" -H "Content-Type: application/x-www-form-urlencoded" -d "event1=&event2=&document="
我不确定如何使 requestBody fields/elements 在 swagger 中显示为可选 UI
您的 API 定义是正确的。这是 Swagger UI 的一个限制,它总是发送所有表单字段,包括具有空值的可选字段。此处跟踪此问题: