Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having a nested object structure in formData and get the same reflected on swagger-ui #4041

Closed
cskru opened this issue Dec 19, 2017 · 7 comments

Comments

@cskru
Copy link

cskru commented Dec 19, 2017

Currently, swagger accepts properties in formData directly. What I intend to do is, formData should be able to accept a nested object and the file will be present somewhere inside. (See example below).
I am not sure if this is a bug or a whole new feature to be added.

Q A
Which Swagger/OpenAPI version? 2.0
Which browser & version? Chrome latest
Which operating system? Windows
formData: 
   { file: 
      { opt: 
         { filename: 'abc.json',
           contentType: 'application/octet-stream' 
          },
        value: <The file contents are to be placed here (octet-stream)> 
       } 
   }

Current Behavior

I tried to do the same like:

            - name: file
              in: formData
              type: object
              properties:
               file:
                   type: object
                   properties:
                       opt:
                          type: object
                          properties:
                              filename:
                                  type: string
                                  example: "abc.json"
                                  required: true
                              contentType:
                                   type: string
                                   example: 'application/octet-stream' 
                       value:
                            type: file

But this doesn't work.
The output is like this:
image

I want to represent formData as a nested object itself and represent it in swagger appropriately.
Is there a way to do so?

Thanks in advance!

@hkosova
Copy link
Contributor

hkosova commented Jan 9, 2018

Is the request body supposed to be sent as application/json, application/x-www-form-urlencoded (form data) or as a multipart request?

@dalbrx-forcam
Copy link
Contributor

dalbrx-forcam commented Jan 31, 2018

I have a similar problem:

The swagger config Version 2.0:

grafik

The swagger UI:

grafik

As you can see the ncElement is rendered as type formData which is a string instead type "createNCElementProperties"

In addition the request part for the ncElement doesn't have a Content-Type. The correct Content-Type would be application/json

grafik

@hkosova
Copy link
Contributor

hkosova commented Feb 1, 2018

@dalbrx-forcam In OpenAPI/Swagger 2.0, formData parameters cannot be objects. In other words, you can POST either JSON (consume application/json) or a file (consume multipart/form-data), but not both in the same request.

Multipart requests with JSON+file are supported in OpenAPI 3.0:

openapi: 3.0.0
...
paths:
  /something:
    post:
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                ncElement:
                  $ref: '#/components/schemas/createNcElementProperties'
                file:
                  type: string
                  format: binary
              required:
                - ncElement
                - file
      responses:
        ...

More examples of OpenAPI 3.0 multipart request definitions:
https://swagger.io/docs/specification/describing-request-body/multipart-requests/
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#special-considerations-for-multipart-content


However, "try it out" support for multipart requests and file upload for OpenAPI 3.0 definitions is not yet available in Swagger UI. I believe this is being tracked in the OAS 3.0 Support Backlog.

@webron
Copy link
Contributor

webron commented Jul 11, 2018

As @hkosova mentioned, Swagger 2.0 does not support it, but OAS3 does. We recently added support for it in swagger-ui, so I'm going to go ahead and close the ticket.

@webron webron closed this as completed Jul 11, 2018
@Cloudmersive
Copy link

Why is this issue closed? This issue is still not fixed. This is actually a very serious issue - it means that you cannot upload both an object and a file.

@Cloudmersive
Copy link

And Swagger UI definitely still doesn't support this.

@hkosova
Copy link
Contributor

hkosova commented Aug 13, 2018

@Cloudmersive Uploading objects + files is supported in OpenAPI 3.0 and should work in latest Swagger UI. Check out the examples in my comment above.

If something does not work for you, open a new issue and provide the steps to reproduce and other details.

@lock lock bot locked and limited conversation to collaborators Aug 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants