swagger-api / swagger-api/swagger-ui

anyOf in requestBody does not show up as optional elements and it takes the inputs wrong

Open
#5,532 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

cat: try-it-out specification: 3.x type: bug
Dominant language
JavaScript
Stars
29k
Forks
9.3k
Avg merge
2d 23h
Merged PRs (30d)
25

Description

  • OS: [Centos7]
  • Browser: [Microsoft Edge]
  • Method of installation: [pip install "connexion[swagger-ui]"]
  • Swagger-UI version: [3.22.3]
  • Swagger/OpenAPI version: [OpenAPI 3.0]

I need to define an API that will consume URL encoded form data and produce JSON data.
so I have defined the requestBody that is of URL encoded form data which can take any one or more of the following strings in the body and here is the snippet:

Example Swagger/OpenAPI definition:

   "components": {
        "requestBodies": {
            "customEventBody": {[enter image description here][1]
                    "content": {
                            "application/x-www-form-urlencoded":{
                                   "schema": {
                                        "anyOf": [
                                             {
                                                "$ref": "#/components/schemas/Interrupt"
                                             },
                                             {
                                                "$ref": "#/components/schemas/injected_val"
                                             },
                                             {
                                                "$ref": "#/components/schemas/document"
                                             }
                                           ]
                                           }
                                    }
                            },
                       }
           },
           "schemas": {
              "Interrupt": {
                 "title": "Interrupt event",
                 "type": "object",
                 "properties": {
                        "Interrupt": {
                                 "type": "boolean"
                             }
                     }
               },
              "injected_val": {
                 "title": "Injected value",
                 "type": "object",
                 "properties": {
                        "injected_val": {
                                 "type": "string"
                             }
                     }
               },
              "document": {
                 "title": "document",
                 "type": "object",
                 "properties": {
                        "document": {
                                 "type": "string"
                             }
                     }
               }
            }
     }
}

generated Curl request when I want to try out the curl command from swagger UI:
curl -X POST "http://xx.yy.zz.ee:8080/api/v1/calls/54544/js" -H "accept: /" -H "Content-Type: application/x-www-form-urlencoded" -d "0='&1=I&2=n&3=t&4=e&5=r&6=r&7=u&8=p&9=t&10='&11=%3D&12='&13=t&14=r&15=u&16=e&17='"

I wanted to see the requestBody elements to appear as optional elements(since I have used anyOf) and I should be able input the requestBody-> element value as shown in the screen shot:

for example:
Interrupt=true
or
injected_val="push_event"
or
document={dev1:"3", dev2="4"}

Screenshots

Swagger UI example screenshot:
swagger_ui_jumbled_url_input

But the value given are all jumbled up

and another issue is when I give no input to some of the elements, it still takes empty value which is not expected

for example - if I do not give any values in swagger-UI for Interrupt and injected_val, the generated curl request is this :
curl -X POST "http://xx.yy.zz.ee:8080/api/v1/calls/54544/js" -H "accept: /" -H "Content-Type: application/x-www-form-urlencoded" -d Interrupt=&injected_val=&

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the OpenAPI 3 requestBody example in Swagger UI with application/x-www-form-urlencoded and anyOf. Inspect the request-body editor and generated curl output; done means anyOf alternatives appear as optional, independently usable inputs and omitted values are not serialized as empty fields.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, openapi
Domain
api, frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.