Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JSON to JSON Schema draft 4 compatible with Swagger 2.0

I've been given some JSON files generated by a REST API with plenty of properties.

I've created a Swagger 2.0 definition for this API and need to give it the corresponding schema for the response.

The main problem: this JSON file has loads of properties. It would take so much time and I would make many mistakes if I write the schema manually. And it’s not the only API I need to describe.

I know there are some tools to convert JSON to JSON schemas but, if I’m not mistaken, Swagger only has $refs to other objects definitions thus only has one level whereas the tools I’ve found only produce tree structured schemas. My question: is there any tool to convert a JSON (or JSON Schema) to a Swagger 2.0 compatible one ?

Note: I'm working in YAML but I wouldn't be an issue, would it ?

For example, what I need:

  List of Movements:
    type: "array"
    items:
      $ref: "#/definitions/Movement"
  Movement:
    properties:
      dateKey:
        type: "string"
      movement:
        $ref: "#/definitions/Stock"
    additionalProperties: false
  Stock:
    properties:
      stkUnitQty:
        type: "string"
      stkDateTime:
        type: "string"
      stkUnitType:
        type: "string"
      stkOpKey:
        type: "string"
    additionalProperties: false

For my JSON document:

[
  {
    "dateKey": "20161110",
    "stkLvls": [
      {
        "stkOpKey": "0",
        "stkUnitType": "U",
        "stkDateTime": "20161110T235010.240+0100",
        "stkUnitQty": 30
      }
    ]
  },
  {
    "dateKey": "20161111",
    "stkLvls": [
      {
        "stkOpKey": "0",
        "stkUnitType": "U",
        "stkDateTime": "20161111T231245.087+0100",
        "stkUnitQty": 21
      }
    ]
  }
  ]

But, what http://jsonschema.net/#/ gives me:

---
"$schema": http://json-schema.org/draft-04/schema#
type: array
items:
  type: object
  properties:
    dateKey:
      type: string
    stkLvls:
      type: array
      items:
        type: object
        properties:
          stkOpKey:
            type: string
          stkUnitType:
            type: string
          stkDateTime:
            type: string
          stkUnitQty:
            type: integer
        required:
        - stkOpKey
        - stkUnitType
        - stkDateTime
        - stkUnitQty
  required:
  - dateKey
  - stkLvls

I'm new to that, but curious, don't hesitate to explain deeply.

Thank you in advance for your help !

like image 633
Cwellan Avatar asked Dec 01 '16 10:12

Cwellan


People also ask

Does swagger use JSON schema?

Swagger supports only subset of JSON Schema Draft 4 Specification of Swagger 1.2 and 2.0 states, it supports only subset of JSON Schema Draft 4 (s. here). This means, that: one cannot rely, that each valid JSON Schema can be completely supported by Swagger.

What is the difference between JSON and swagger?

JSON Server belongs to "API Tools" category of the tech stack, while Swagger UI can be primarily classified under "Documentation as a Service & Tools". "Stupid simple" is the top reason why over 2 developers like JSON Server, while over 33 developers mention "Open Source" as the leading cause for choosing Swagger UI.

Is OpenAPI a JSON schema?

OpenAPI 3.0 data types are based on an extended subset JSON Schema Specification Wright Draft 00 (aka Draft 5). The data types are described using a Schema object.

What is JSON schema additionalProperties?

The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword. By default any additional properties are allowed.


2 Answers

I also needed a converter tool and came across this. So far it seems to work pretty well. It does both JSON and YAML formats.

https://swagger-toolbox.firebaseapp.com/

Given this JSON (their sample):

{
  "id": 1,
  "name": "A green door",
  "price": 12,
  "testBool": false,
  "tags": [
    "home",
    "green"
  ]
}

it generated this:

{
    "required": [
        "id",
        "name",
        "price",
        "testBool",
        "tags"
    ],
    "properties": {
        "id": {
            "type": "number"
        },
        "name": {
            "type": "string"
        },
        "price": {
            "type": "number"
        },
        "testBool": {
            "type": "boolean"
        },
        "tags": {
            "type": "array",
            "items": {
                "type": "string"
            }
        }
    }
}
like image 67
mateuscb Avatar answered Oct 25 '22 23:10

mateuscb


I know there are some tools to convert JSON to JSON schemas but, if I’m not mistaken, Swagger only has $refs to other objects definitions thus only has one level

You are mistaken. Swagger will respect any valid v4 JSON schema, as long as it only uses the supported subset.

The Schema Object...is based on the JSON Schema Specification Draft 4 and uses a predefined subset of it. On top of this subset, there are extensions provided by this specification to allow for more complete documentation.

It goes on to list the parts of JSON schema which are supported, and the bits which are not, and the bits which are extended by swagger.

like image 44
tom redfern Avatar answered Oct 26 '22 00:10

tom redfern