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 (null, number, integer, string, object, array) of non-predefined depth.
Unfortunately, it seems that Swagger 2.0 is quite strict on schemas for input and output and does not allow the whole set of schemas allowed by JSON Schema. The Swagger editor doesn't allow for example mixed values (for example a property that can be either a boolean or an integer) or loosely defined arrays (the type of items must be strictly defined) and objects.
So I'm trying a workaround by defining a MixedValue
schema:
---
swagger: '2.0'
info:
version: 0.0.1
title: Data store API
consumes:
- application/json
produces:
- application/json
paths:
/attributes/{attrId}/value:
parameters:
- name: attrId
in: path
type: string
required: true
get:
responses:
'200':
description: Successful.
schema:
$ref: '#/definitions/MixedValue'
put:
parameters:
- name: value
in: body
required: true
schema:
$ref: '#/definitions/MixedValue'
responses:
responses:
'201':
description: Successful.
definitions:
MixedValue:
type: object
properties:
type:
type: string
enum:
- 'null'
- boolean
- number
- integer
- string
- object
- array
boolean:
type: boolean
number:
type: number
integer:
type: integer
string:
type: string
object:
description: deep JSON object
type: object
additionalProperties: true
array:
description: deep JSON array
type: array
required:
- type
But the Swagger Editor refuses the loosely defined object
and array
properties.
Questions: - Is there a way to workaround this issue? - Is it just a Swagger Editor bug or a strong limit of the Swagger 2.0 spec? - Is there a better way (best practice) to specify what I need? - Can I expect some limitations in code produced by swagger for some languages with my API spec?