在swaggerUI中将requestBody字段设置为可选

时间:2019-08-12 01:24:05

标签: json swagger-ui connexion

为我的flask python项目使用以下版本:

connexion == 2.3.0  swagger-ui-bundle == 0.0.5  OAS 3.0

我想使所有requestBody字段为可选,现在我定义了json,如下所示,并且我假设如果您未在requestBody中提供必填字段,则应将其视为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字段/元素在swagger UI中显示为可选

1 个答案:

答案 0 :(得分:0)

您的API定义正确。 Swagger UI的局限性在于它始终发送所有表单字段,包括具有空值的可选字段。在此跟踪此问题:

https://github.com/swagger-api/swagger-ui/issues/5303