Swagger 2.0: what schema to accept any (complex) JSON value

大城市里の小女人 提交于 2019-11-26 23:16:41

An arbitrary-type schema can be defined like this:

definitions:
  AnyValue: {}

or if you want a description:

definitions:
  AnyValue:
    description: 'Can be anything: string, number, array, object, etc.'

Without a defined type, AnyValue can be anything - string, number, boolean, array, object, etc. See this Q&A for more details on how type-less schemas work.

In OpenAPI 3.0 you can add nullable: true to allow the null value:

components:
  schemas:
    AnyValue:
      nullable: true
      description: Can be anything, including null.


Here's how Swagger Editor 2.0 handles a body parameter with the AnyValue schema:

I don't know how code generators handle this though.

Maybe this is what your are looking for "Patterned Objects":

Field Pattern: ^x-

Type: Any

Description: Allows extensions to the Swagger Schema. The field name MUST begin with x-, for example, x-internal-id. The value can be null, a primitive, an array or an object. See Vendor Extensions for further details.

Source: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md

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