Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding not exploding a property in requestBody in swagger-ui

The parameter is not being exploded into separate fields, and i cant get my head wrapped around why.

This is my yaml, using OpenApi 3.0

paths:
  /match/started:
    post:
      tags:
        - match
      summary: 'Callback for when a game has started.'
      operationId: 'App\Http\Controllers\Api\V1\MatchController::started'
      requestBody:
        description: 'Something something batman!'
        required: true
        content:
          multipart/form-data:
            schema:
              required:
                - match_uuid
              properties:
                game_uuid:
                  type: string
                player_uuids:
                  type: array
                  items:
                    type: string
              type: object
            encoding:
              player_uuids:
                style: form
                explode: true
      responses:
        200:
          description: 'success response'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Api_V1_Match_Started'

this is the curl request swagger is giving me (BAD)

curl -X POST "https://editor.swagger.io/api/v1/match/started" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "game_uuid=test" -F "player_uuids=aaa,bbb,ccc"

where you can see the last parameter is -F "player_uuids=aaa,bbb,ccc" and it should be -F "player_uuids=aaa" -F "player_uuids=bbb" -F "player_uuids=ccc"

so the full request should look like this:

curl -X POST "https://editor.swagger.io/api/v1/match/started" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "game_uuid=test" -F "player_uuids=aaa" -F "player_uuids=bbb" -F "player_uuids=ccc"

like image 347
Knobik Avatar asked Jan 04 '19 08:01

Knobik


People also ask

What is Requestbody in swagger?

In Swagger terms, the request body is called a body parameter. There can be only one body parameter, although the operation may have other parameters (path, query, header).

What is explode in swagger?

An optional explode keyword controls the array and object serialization. Given the cookie named id , the cookie value is serialized as follows: style.

What is oas3 in swagger?

The OpenAPI Specification (OAS) defines a standard, language-agnostic interface to RESTful APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection.


1 Answers

There's currently no way to define your scenario (a multipart request with an exploded array) with OpenAPI, because the explode and style behavior is only defined for application/x-www-form-urlencoded but not for multipart/*:

style
... This property SHALL be ignored if the request body media type is not application/x-www-form-urlencoded.

explode
... This property SHALL be ignored if the request body media type is not application/x-www-form-urlencoded.

Related discussion: Swagger UI: Submit An Array of Integer Elements In formdata

You might want to file an enhancement request with the OpenAPI Spec.

like image 146
Helen Avatar answered Sep 28 '22 10:09

Helen