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

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


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

# swagger: '2.0'definitions:  AnyValue: {}# openapi: 3.0.0components:  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.0components:  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.


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