Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of objects as an input parameter in swagger

I'm trying to describe the following post parameter in swagger:

{
    "sources": [
        {
            "id": 101,
            "parentId": 201
        },{
            "id": 102,
            "parentId": 201
        },{
            "id": 102,
            "parentId": 202
        }
    ],
    "destinationId": 301,
    "param1": "value 1",
    "param2": "value 2",
}

The issue is that the sources is an array of objects, that swagger does not seem to support. Here is what I tried:

paths:
    /bulk-action:
        post:
            parameters:
                - name: sources
                  in: formData
                  type: array
                  enum:
                      $ref: '#/definitions/BulkSource'
                - name: destinationId
                  in: formData
                  type: integer
                - name: param1
                  in: formData
                  type: string
                - name: param2
                  in: formData
                  type: string
definitions:
    BulkSource:
        type: object
        properties:
            id:
                type: integer
            parentId:
                type: integer

Any idea on how to work around this limitation?

like image 327
maphe Avatar asked Dec 03 '22 23:12

maphe


1 Answers

If I understand correctly, your request body to post is a json object instead of form. In such case, your swagger document need to be modified as follows:

  1. When request body is json, a parameter with in: body is used instead of multiple parameters of in: formData.
  2. If in is body, a schema object is required.
  3. Defined the json properties under schema. If the property type is array, items object is required.

Following is an example:

paths:
  /bulk-action:
    post:
      consumes:
        - application/json
      parameters:
        - name: body
          in: body
          schema:
            properties:
              sources:
                type: array
                items:
                  $ref: '#/definitions/BulkSource'
              destinationdId:
                type: integer
      responses:
        200:
          description: OK
definitions:
  BulkSource:
    type: object
    properties:
      id:
        type: integer
      parentId:
        type: integer
like image 80
Wilson Avatar answered Dec 31 '22 09:12

Wilson