swagger-api / swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
https://swagger.io
Apache License 2.0
26.46k stars 8.95k forks source link

Error when optional formData field is not provided using Swagger UI POST Request #8326

Open JessNah opened 1 year ago

JessNah commented 1 year ago

Hello, I am using swagger version 4.12.0. OpenAPI Spec 2.0. I have an API POST call where I am sending a formData file type OPTIONAL param in the request. But when using the swagger ui, for the case where I do not select any file for this optional param field. I receive this error: "[java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -8190". The request works when I select a file for the request. It seems to fail before it even reaches my backend code.

I was able to reproduce what I think is the same error on the swagger sample editor: Steps to reproduce:

  1. Open https://editor.swagger.io/
  2. Click Edit > Choose Load Petstore OAS 2.0
  3. On the right panel, try out POST /pet (Change first property id: from 0 to 100 , leave other fields as is) hit enter
  4. Go to post /pet/{petId}/uploadImage Try it out, set petId to 100, leave other formData fields empty. Hit enter
  5. See response: "message": "org.jvnet.mimepull.MIMEParsingException: Missing start boundary"
  6. On left side code panel, ctrl + F “/pet/{petId}/uploadImage:” the formData fields are both required: false.

This is actually a regression, as before I updated to version 4.12.0, this same API used to work without any errors. I guess at some version level, some additional checking was added to the params where this issue was introduced.

It looks like this fail might be because even though I do not add this optional formData field, the CURL request swagger UI sends still includes this header -> -H 'Content-Type: multipart/form-data'. When I try the curl command myself I see the same error, but if I remove this multipart/form-data contentType header field from the Curl request. It then succeeds.

Thanks a lot for taking the time to look at this, hope this could be resolved..

    "/myAPI":{
      "post":{
        "summary":"mySummary",
        "description":"myDescription",
        "operationId":"myAPIId",
        "x-display-name":"My API",
        "tags":[
          "My API"
        ],
        "produces":[
          "application/json"
        ],
        "parameters":[
          {
            "name":"name",
            "in":"query",
            "description":"name field",
            "required":true,
            "type":"string" 
          },       
          {
            "name":"file",
            "in":"formData",
            "description":"Upload file",
            "required":false,
            "type":"file" 
          }
        ],
JessNah commented 1 year ago

Posting to request if there has been any update or investigation on the above issue.

JessNah commented 1 year ago

Posting to note, that this is still an issue we are looking to get addressed.