OpenAPI query string parameter with list of objects

不打扰是莪最后的温柔 提交于 2019-12-18 08:14:45

问题


I am trying to document with OpenAPI a query string which look like

filtered[0][id]=code&filtered[0][value]=12345

and contains a list of object with properties id and value.

My yaml documentation looks like the following

parameters:
    - name: filtered
      in: query
      description: filters to be applied
      explode: true
      style: deepObject
      schema:
        type: array
        items:
          properties:
            id:
              description: name of the field to be filtered
              type: string
            value:
              description: value of the filter
          type: object

The problem is the following: it looks like the style: deepObject option works only for one level, and not at the second level where my objects actually are. That is, it expects a query string like

?sorted[0]=%7B%0A%20%20%22id%22%3A%20%22string%22%2C%0A%20%20%22value%22%3A%20true%0A%7D

with the object not serialized as an array with id and value keys.

Is there a way to solve this?


回答1:


This is not possible as of OpenAPI 3.0.2.

OpenAPI 3.0 Specification currently defines the deepObject behavior only for simple objects (with primitive properties) such as

{
  "id": 5,
  "name": "Bob"
}

but not for arrays and not for nested objects.

Since the behavior for arrays and nested objects is not defined, there's really no way to describe your query string. Technically, the only way would be to define filtered[0][id], filtered[0][value], etc. as individual query parameters.


If you are designing a new API (rather than describing an existing one), consider passing the array of objects in the request body instead.




回答2:


As name implies, :), deepObject style only "provides a simple way of rendering nested objects", not arrays. At the least according to Version 3.0.1 it applies only to objects.

Note that even nested objects might be not yet well supported by tools because the specification "does not provide such examples".

So you format is not compatible with Open API, yet may be you can define you query as parameters which follow a regex. I such cases usually I do my best yet provide some detailed explanation (which of course programmers typically skip)

https://swagger.io/specification/



来源:https://stackoverflow.com/questions/52892768/openapi-query-string-parameter-with-list-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!