Search code examples
jsonswagger-uiconnexion

make requestBody fields optional in swaggerUI


Using the following versions for my flask python project:

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

I want to make all the requestBody fields optional, right now I have defined the json as shown below and I have assumed if you do not provide required field in the requestBody, then it should be taken as false and if I do not provide any values in swagger UI for these fields it should NOT generate curl request with those fields with -d option.


    "openapi": "3.0.0",
    "info": {
        "description": "Dev",
        "version": "1.0.0",
        "title": "DEV-API",
        "contact": {
        "email": "[email protected]"
        },
        "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"
                    }
                }
            }
        }
    }

But it does generate with no Values as follows:

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="

I am not sure how to make the requestBody fields/elements to appear as optional in swagger UI


Solution

  • Your API definition is correct. It's a limitation of Swagger UI that it always sends all form fields, including optional fields with empty values. This issue is tracked here:

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