swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Arrays are sent incorrectly in `multipart/form-data`

Open FlameWolf opened this issue 1 year ago • 3 comments

Q&A (please complete the following information)

  • OS: Windows 11
  • Browser: Firefox
  • Version: 130
  • Method of installation: npm
  • Swagger-UI version: 5.18.2
  • Swagger/OpenAPI version: OpenAPI 3.0

Content & configuration

Example Swagger/OpenAPI definition:

openapi: 3.0.1
info:
  title: DotNet9WebApi
  version: '1.0'
paths:
  /WeatherForecast/post:
    post:
      tags:
        - DotNet9WebApi
      requestBody:
        content:
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/PostBody'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/PostBody'
      responses:
        '200':
          description: OK
components:
  schemas:
    PostBody:
      type: object
      properties:
        content:
          type: string
          nullable: true
        intArrProp:
          type: array
          items:
            type: integer
            format: int32
          nullable: true
      additionalProperties: false

Describe the bug you're encountering

When submitting an array as multipart/form-data content, the values are sent as a comma-separated list, instead of as multiple form parts with the same name.

To reproduce...

Steps to reproduce the behavior:

  1. Go to https://editor-next.swagger.io/
  2. Paste the above sample YAML into the editor
  3. Try out the /WeatherForecast/post endpoint
  4. Add 2 or more items into the intArrProp array using the Add integer item button
  5. Click Execute and observe the resulting curl

Expected behavior

The curl should look like this:

curl -X 'POST' \
  'https://editor-next.swagger.io/WeatherForecast/post' \
  -H 'accept: */*' \
  -H 'Content-Type: multipart/form-data' \
  -F 'content=string' \
  -F 'intArrProp=1' \
  -F 'intArrProp=2' \
  -F 'intArrProp=3'

Actual behavior

The curl looks like this:

curl -X 'POST' \
  'https://editor-next.swagger.io/WeatherForecast/post' \
  -H 'accept: */*' \
  -H 'Content-Type: multipart/form-data' \
  -F 'content=string' \
  -F 'intArrProp=1,2,3'

Screenshots

image

FlameWolf avatar Nov 21 '24 12:11 FlameWolf

Same issue. It seems that switching to Swagger 2 (OpenAPI 2) fixes this.

palapapa avatar Dec 01 '24 10:12 palapapa

Hello there! You can specify the formatting of given property values by using an encoding object. In this specific scenario, the schema should look like this:

openapi: 3.0.1
info:
  title: DotNet9WebApi
  version: '1.0'
paths:
  /WeatherForecast/post:
    post:
      tags:
        - DotNet9WebApi
      requestBody:
        content:
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/PostBody'
            encoding: 
                intArrProp:
                    explode: true
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/PostBody'
      responses:
        '200':
          description: OK
components:
  schemas:
    PostBody:
      type: object
      properties:
        content:
          type: string
          nullable: true
        intArrProp:
          type: array
          items:
            type: integer
            format: int32
          nullable: true
      additionalProperties: false

ref: https://swagger.io/specification/#media-type-object

robert-hebel-sb avatar Feb 28 '25 18:02 robert-hebel-sb

https://github.com/swagger-api/swagger-ui/issues/3494#issuecomment-3379154976

this worked as well.

saddam008h avatar Oct 08 '25 00:10 saddam008h