Bill
Bill

Reputation: 3209

Swagger/OpenAPI spec for arrays of objects in URL query parameter

Assume I have a URL which takes a path of: ?filter[id]=1&filter[name]=bob&order[][name]=asc&order[][age]=desc

How would one be able to convert this into swagger documentation, specifically, array of objects and arrays as the query parameter.

Upvotes: 4

Views: 4300

Answers (2)

Happy Coder
Happy Coder

Reputation: 1423

It's an old question, but the answer above is misleading

array of objects s is not supported in OpenAPI 3.0/3.1 Specifications currently defines

see the question OpenAPI query string parameter with list of objects

Upvotes: 0

Helen
Helen

Reputation: 98052

Your example is not an array of objects but two separate object parameters - filter and order[], each serialized using the deepObject style (supported in OpenAPI 3.0). You can describe these parameters as follows:

openapi: 3.0.2
...

paths:
  /something:
    get:
      # ?filter[id]=1&filter[name]=bob&order[][name]=asc&order[][age]=desc
      parameters:
        - in: query
          name: filter
          schema:
            type: object
            properties:
              id:
                type: integer
                example: 1
              name:
                type: string
                example: bob
          style: deepObject

        - in: query
          name: order[]
          schema:
            type: object
            properties:
              name:
                type: string
                example: asc
              age:
                type: string
                example: desc
          style: deepObject

Upvotes: 4

Related Questions