Skip to content

OAS 3: Support for uploading an array of files in multipart requests #4600

Closed

Description

@hkosova

Swagger UI 3.16.0 added support for binary file upload using multipart requests. It works if a multipart request uses files as specific named fields, but doesn't work if the request uses an array of files.

Q&A (please complete the following information)

  • OS: Windows 7
  • Browser: Chrome 66
  • Method of installation: http://petstore.swagger.io, also using a local copy of Editor.
  • Swagger-UI version: 3.16.0
  • Swagger/OpenAPI version: OpenAPI 3.0

Content & configuration

This spec is based on an example from the OpenAPI 3.0.1 Specification:

openapi: 3.0.1
info:
 title: Multiple file upload test
 version: 0.0.0
servers:
  - url: http://httpbin.org
paths:
 /post:
   post:
     requestBody:
       content:
         multipart/form-data:
           # This works:
           # schema:
           #   type: object
           #   properties:
           #     upload:
           #       type: string
           #       format: binary
           
           # This doesn't work:
           schema:
             type: object
             properties:
               # The property name 'file' will be used for all files.
               file:
                 type: array
                 items:
                   type: string
                   format: binary
     responses:
       '200':
         description: OK

Is your feature request related to a problem?

Yes, clicking "try it out" shows "😱 Could not render this component, see the console." The console errors are:

TypeError: Cannot read property 'inferSchema' of undefined
    at t.value (swagger-ui.js:1)
    at t.render (swagger-ui.js:1)
    at u._renderValidatedComponentWithoutOwnerOrContext (ReactCompositeComponent.js:796)
    at u._renderValidatedComponent (ReactCompositeComponent.js:819)
    at u.performInitialMount (ReactCompositeComponent.js:359)
    at u.mountComponent (ReactCompositeComponent.js:255)
    at Object.mountComponent (ReactReconciler.js:43)
    at u.performInitialMount (ReactCompositeComponent.js:368)
    at u.mountComponent (ReactCompositeComponent.js:255)
    at Object.mountComponent (ReactReconciler.js:43)

TypeError: Cannot read property 'inferSchema' of undefined
    at t.value (swagger-ui.js:1)
    at t.render (swagger-ui.js:1)
    at u._renderValidatedComponentWithoutOwnerOrContext (ReactCompositeComponent.js:796)
    at u._renderValidatedComponent (ReactCompositeComponent.js:819)
    at u._updateRenderedComponent (ReactCompositeComponent.js:743)
    at u._performComponentUpdate (ReactCompositeComponent.js:721)
    at updateComponent (ReactCompositeComponent.js:642)
    at u.receiveComponent (ReactCompositeComponent.js:544)
    at Object.receiveComponent (ReactReconciler.js:122)
    at u._updateRenderedComponent (ReactCompositeComponent.js:751)

😱

Describe the solution you'd like

Clicking "try it out" presents a form where we can upload multiple files.

Describe alternatives you've considered

N/a

Additional context

This is (sort of) related to #3641 "OAS 3.0 Support Backlog".

Activity

  1. tuxedo0801 commented on Jul 24, 2018

    @tuxedo0801

    I'm having the same issue here on swaggerhub.
    Any updat on this?

  2. cedric-jonas-kisters commented on Aug 4, 2018

    @cedric-jonas-kisters

    Works for me actually.

  3. hkosova commented on Aug 6, 2018

    @hkosova
    ContributorAuthor

    @cedric-c84-eu It looks like file array upload is not implemented yet. The file inputs are displayed now, but the actual request contains the string [object File] instead of the file contents.

    curl -X POST "http://httpbin.org/post" -H "accept: */*" -H "Content-Type: multipart/form-data" -F "file=[object File],[object File]"
    
    POST http://httpbin.org/post
    Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryIk1aWkovTmf7LSJX
    
    ------WebKitFormBoundaryIk1aWkovTmf7LSJX
    Content-Disposition: form-data; name="file"
    
    [object File],[object File]
    ------WebKitFormBoundaryIk1aWkovTmf7LSJX--
    
  4. malutanpetronel commented on Oct 31, 2018

    @malutanpetronel

    I dd in laravel the result of this and get ...characters">[object File],[object File]"...

  5. bleuscyther commented on Dec 12, 2018

    @bleuscyther
  6. lcahlander commented on Jan 28, 2019

    @lcahlander

    It is failing in curlify.js

            if (v instanceof win.File) {
              curlified.push( `"${k}=@${v.name}${v.type ? `;type=${v.type}` : ""}"` )
            } else {
              curlified.push( `"${k}=${v}"` )
            }

    curlify is not taking the array into account

  7. default-writer commented on May 2, 2019

    @default-writer

    probably it is better to take a blob

  8. mansuryan commented on Aug 15, 2019

    @mansuryan

    Is there any news???
    The same problem for me

  9. 0s3v3n0 commented on Aug 22, 2019

    @0s3v3n0

    Hello is there any progress ? its opened more than year ...
    The same problem for me

  10. webron commented on Aug 22, 2019

    @webron
    Contributor

    As always, PRs are welcome.

  11. jmfernandez commented on Sep 30, 2019

    @jmfernandez

    It will be more than welcome when it is ready

  12. Darkzarich commented on Oct 1, 2019

    @Darkzarich

    I did everything following documentation and it did work and did send files like this:

    ------WebKitFormBoundarykREvSfNcPTrA7BI7
    Content-Disposition: form-data; name="attachments"
    
    [object File],[object File],[object File]
    ------WebKitFormBoundarykREvSfNcPTrA7BI7--
    
    

    However, my backend app (Node.js and Multer for uploading files) was unable to see it.

    I tried to upload files with javascript using FormData class like this:

    const formData = new FormData();
    formData.append('attachments', this.file[0]);
    formData.append('attachments', this.file[1]);
    formData.append('attachments', this.file[2]);
    

    And it worked as expected.

    I'm not sure if this header swagger ui sends is right or wrong but I think this is the reason. Sending single files works perfectly

  13. 14 remaining items

  14. dfeinzeig commented on May 21, 2020

    @dfeinzeig

    I just tried PR #5999 but still does not work. The input type is now text instead of file, so you can't choose files. If I change it to file it just posts a list of filenames, but no files.

    image

  15. hkosova commented on May 21, 2020

    @hkosova
    ContributorAuthor

    @anzoman @dfeinzeig Uploading arrays of files is not supported yet.

  16. dfeinzeig commented on May 21, 2020

    @dfeinzeig

    @hkosova , can you point me at any relevant parts of the code that would need to be updated to support it?

  17. hkosova commented on May 21, 2020

    @hkosova
    ContributorAuthor

    @dfeinzeig

    can you point me at any relevant parts of the code that would need to be updated to support it?

    #4600 (comment)

  18. webhacking commented on May 25, 2020

    @webhacking

    Any news ?

  19. tim-lai commented on May 27, 2020

    @tim-lai
    Contributor

    @hkosova @anzoman @dfeinzeig @webhacking multiple file upload using OAS3.0 and arrays is now properly supported in the most recent SwaggerClient release (v3.25.4). Here's my sample definition:

    {
      "openapi": "3.0.0",
      "servers": [
        {
          "url": "http://localhost:3300/api/v1"
        }
      ],
      "info": {
        "version": "1",
        "title": "MULTI PART TEST - OAS3",
        "description": ""
      },
      "paths": {
        "/land/content/ViewOfAuthOwner": {
          "post": {
            "summary": "",
            "requestBody": {
              "content": {
                "multipart/form-data": {
                  "schema": {
                    "type": "object",
                    "properties": {
                      "hhlContent:sort": {
                        "description": "",
                        "default": "id",
                        "type": "string",
                        "enum": [
                          "id",
                          "title"
                        ]
                      },
                      "hhlContent:order": {
                        "description": "",
                        "default": "desc",
                        "type": "string",
                        "enum": [
                          "asc",
                          "desc"
                        ]
                      },
                      "email[]": {
                        "description": "The list of emails.",
                        "type": "array",
                        "items": {
                          "type": "string"
                        }
                      }
                    }
                  },
                  "encoding": {
                    "style": "pipeDelimited",
                    "explode": false
                  }
                }
              }
            },
            "responses": {
              "200": {
                "description": "",
                "content": {
                  "application/json": {
                    "schema": {
                      "type": "object"
                    }
                  }
                }
              }
            }
          }
        },
        "/land/content/uploadImage": {
          "post": {
            "summary": "upload image(s)",
            "requestBody": {
              "content": {
                "multipart/form-data": {
                  "schema": {
                    "type": "object",
                    "properties": {
                      "imageId": {
                        "description": "",
                        "default": "id",
                        "type": "string"
                      },
                      "images[]": {
                        "description": "The list of files",
                        "type": "array",
                        "items": {
                          "type": "file",
                          "format": "binary"
                        }
                      }
                    }
                  }
                }
              }
            },
            "responses": {
              "200": {
                "description": "",
                "content": {
                  "application/json": {
                    "schema": {
                      "type": "object"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

    screenshots (live local server response):
    oas3-multiple-files-request
    oas3-multiple-files-response

  20. ffroliva commented on May 27, 2020

    @ffroliva

    What version of org.springdoc should I need to update to?

    I am currently working with version 1.3.4.

    	<parent>
    		<groupId>org.springdoc</groupId>
    		<artifactId>springdoc-openapi</artifactId>
    		<version>1.3.4</version>
    	</parent>
    

    Regards,

    FO

  21. hkosova commented on May 28, 2020

    @hkosova
    ContributorAuthor

    @tim-lai OAS3 does not have type: file, it uses type: string + format: binary. Use this definition instead:

    openapi: 3.0.0
    servers:
      - url: 'http://localhost:3300/api/v1'
    info:
      version: '1'
      title: MULTI PART TEST - OAS3
    paths:
      /land/content/uploadImage:
        post:
          summary: upload image(s)
          requestBody:
            content:
              multipart/form-data:
                schema:
                  type: object
                  properties:
                    imageId:
                      description: ''
                      default: id
                      type: string
                    'images[]':
                      description: The list of files
                      type: array
                      items:
                        type: string    # <----------------
                        format: binary
          responses:
            '200':
              description: ''

    The images[] array is displayed as text inputs instead of file inputs:
    image

  22. Mahalaxmi-13 commented on May 28, 2020

    @Mahalaxmi-13

    @tim-lai What is the maven dependency you use for SwaggerClient release (v3.25.4)

  23. webron commented on May 28, 2020

    @webron
    Contributor

    @Mahalaxmi-13 We don't supply any maven dependencies that wrap our javascript projects.

  24. ffroliva commented on May 29, 2020

    @ffroliva

    @webron

    this dependency should solve my problem:

    <!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-ui -->
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.3.9</version>
    </dependency>
    

    image

  25. JustMik commented on May 29, 2020

    @JustMik
    Contributor

    @hkosova @tim-lai I've fixed upload array file items for type string format binary in this PR #6040

  26. tim-lai commented on Jun 2, 2020

    @tim-lai
    Contributor

    @hkosova closing issue, per resolution with PR #6040. I also fixed the SwaggerClient sample definition.

  27. KuldeepPCodes commented on Jan 18, 2021

    @KuldeepPCodes

    hi, i am facing the same problem in flask_swager_ui , I can not upload array of files files from swagger , when doing request.files i am getting an empty dictionary but i m sending the data from Swagger-UI .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions