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

前端 未结 2 501
执念已碎
执念已碎 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:11

    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

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题