OpenAPI: what schema to accept any (complex) JSON value

前端 未结 2 609
执念已碎
执念已碎 2020-11-28 16:43

The API for which I\'m writing a Swagger 2.0 specification is basically a store for any JSON value.

I want a path to read value and a path to store any JSON values (

2条回答
  •  余生分开走
    2020-11-28 17:19

    An arbitrary-type schema can be defined using an empty schema {}:

    # swagger: '2.0'
    definitions:
      AnyValue: {}
    
    # openapi: 3.0.0
    components:
      schemas:
        AnyValue: {}
    

    or if you want a description:

    # swagger: '2.0'
    definitions:
      AnyValue:
        description: 'Can be anything: string, number, array, object, etc. (except `null`)'
    
    # openapi: 3.0.0
    components:
      schemas:
        AnyValue:
          description: 'Can be anything: string, number, array, object, etc., including `null`'
    

    Without a defined type, a schema allows any values. Note that OpenAPI 2.0 Specification does not support null values, but some tools might support nulls nevertheless.

    In OpenAPI 3.0, type-less schemas allow null values unless nulls are explicitly disallowed by other constraints (such as an enum).

    See this Q&A for more details on how type-less schemas work.


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

    SwaggerEditor: Testing a PUT request with an arbitrary-type body

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

提交回复
热议问题