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.67k stars 8.97k forks source link

Input fields for arrays of references in request body #4975

Open IonelLupu opened 6 years ago

IonelLupu commented 6 years ago

Q&A (please complete the following information)

Content & configuration

Swagger/OpenAPI definition:

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Hotels
paths:
  /hotel:
    post:
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                name:
                  type: string
                images:
                  type: array
                  items:
                    type: string
                    format: binary
                rooms:
                  type: array
                  items:
                    $ref: '#/components/schemas/Room'
      responses:
        '200':
          description: response
          content:
            application/json:
              schema:
                type: string
components:
  schemas:
    Room:
      type: object
      properties:
        name:
          type: string
        image:
          type: string
          format: binary

Swagger-UI configuration options:

SwaggerUI({
  // Auto-configured through bundle
})

Screenshots

image

How can we help?

As you can see in the image there are input fields for the images parameter but not for the rooms parameters. These are in JSON format. It is possible to have input fields for each element in the arrays? Also, I seems the data is not sent in the proper format. I want to send to the server a request in this format(from Chrome Header/Form Data):

------WebKitFormBoundarynP2Wq5aK1GFB5j8h
Content-Disposition: form-data; name="name"

Hotel 1
------WebKitFormBoundarynP2Wq5aK1GFB5j8h
Content-Disposition: form-data; name="rooms[0][name]"

First room
------WebKitFormBoundarynP2Wq5aK1GFB5j8h
Content-Disposition: form-data; name="rooms[0][image]"; filename="img1.jpg"
Content-Type: application/octet-stream

<image data here>
------WebKitFormBoundarynP2Wq5aK1GFB5j8h
Content-Disposition: form-data; name="rooms[1][name]"

Second  room
------WebKitFormBoundarynP2Wq5aK1GFB5j8h
Content-Disposition: form-data; name="rooms[1][image]"; filename="img2.jpg"
Content-Type: application/octet-stream

<image data here>
------WebKitFormBoundarynP2Wq5aK1GFB5j8h--

But Swagger is sending it in this format:

------WebKitFormBoundaryB71611XhkBfcjbca
Content-Disposition: form-data; name="name"

Hotel 1
------WebKitFormBoundaryB71611XhkBfcjbca
Content-Disposition: form-data; name="rooms"

{
  "name": "string",
  "image": "string"
},{
  "name": "string",
  "image": "string"
}
------WebKitFormBoundaryB71611XhkBfcjbca--
hkosova commented 4 years ago

I want to send to the server a request in this format(from Chrome Header/Form Data):

------WebKitFormBoundarynP2Wq5aK1GFB5j8h
Content-Disposition: form-data; name="name"

Hotel 1
------WebKitFormBoundarynP2Wq5aK1GFB5j8h
Content-Disposition: form-data; name="rooms[0][name]"

First room
------WebKitFormBoundarynP2Wq5aK1GFB5j8h
Content-Disposition: form-data; name="rooms[0][image]"; filename="img1.jpg"
Content-Type: application/octet-stream

<image data here>
------WebKitFormBoundarynP2Wq5aK1GFB5j8h
Content-Disposition: form-data; name="rooms[1][name]"

Second  room
------WebKitFormBoundarynP2Wq5aK1GFB5j8h
Content-Disposition: form-data; name="rooms[1][image]"; filename="img2.jpg"
Content-Type: application/octet-stream

<image data here>
------WebKitFormBoundarynP2Wq5aK1GFB5j8h--

OpenAPI Specification does not provide a way to define such requests.

Field names like rooms[1][name] resemble the deepObject serialization style, which can be applied to multipart/* form fields in the next version of the spec, OpenAPI 3.1. However, deepObject behavior is defined only for simple non-nested objects, but not for arrays and not for nested objects.The rooms field in your example is an array (array of Room), so it's not covered by what deepObject currently supports.

This is a limitation of the OpenAPI Specification, not Swagger UI.

hkosova commented 4 years ago

As for the first question

It is possible to have input fields for each element in the arrays?

see https://github.com/swagger-api/swagger-ui/issues/2771#issuecomment-288471955

IonelLupu commented 4 years ago

@hkosova I honestly forgot what was the problem here. It has been almost two years. You can close it if you think the issues is fixed

nsourov commented 3 years ago

The issue is not fixed yet. multipart/form-data still cannot render a nested form for an array of objects. Only works with an array of strings.