Converting JSON to a Swagger 2.0 compatible JSON 4 schema project

I was provided with some JSON files created by the REST API with a lot of properties.

I created a Swagger 2.0 definition for this API and must provide it with the appropriate schema for the response.

The main problem: this JSON file has many properties. This will take a lot of time, and I would make many mistakes if I wrote the diagram manually. And this is not the only API I need to describe.

I know that there are some tools for converting JSON to JSON schemes, but if Im not mistaken, Swagger has only $ refs to define other objects, so there is only one level, while Ive tools find that they produce only structured tree schemes. My question is: is there any tool to convert JSON (or JSON schemas) to compatible with Swagger 2.0?

Note. I work in YAML, but I would not be a problem, right?

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 am new to this, but curiously, feel free to explain deeply.

Thank you in advance for your help!

+5
source share
2 answers

I know that there are some tools for converting JSON to JSON schemes, but if I'm not mistaken, Swagger has only $ refs to define other objects in this way, it has only one level

You are wrong. Swagger will respect any valid V4 JSON scheme if it uses only a supported subset .

The schema object ... is based on the draft JSON 4 specification specification and uses a predefined subset . In addition to this subset, there are extensions provided by this specification to provide more complete documentation.

The following are parts of the supported JSON scheme and bits that are not, and bits that are expanded using swagger.

+3
source

I also need a converter tool and came across this. So far it works very well. It supports 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" ] } 

he 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" } } } } 
+10
source

Source: https://habr.com/ru/post/1260637/


All Articles