4

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?

1 Answer 1

17

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
Sign up to request clarification or add additional context in comments.

6 Comments

could you please tell me where you are making these changes. Or what I want to ask is, in which file the above code is written. I am unable to find the above code in my swagger API.
What if my request body' array does not have the title or name similar to 'Sources' present in the OP body???? I have similar problem, only my request body does not have the name of array. It is more similar to [ { key: value, key: value }, { key: value, key: value } ]
@Rishikesh have you figured out how to do that?
@esh Yes, I figured it for OpenApi 3.0.1, you just have to ignore the title or name and continue with the type field. For 2.0, I still haven't got any solution...
@Wilson can you assist me with this. am trying to collect form array in swaggerPHP, dont know how to define it so swagger can render it well,
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.