Converting from jsonschema to swagger 2.0

user4387602 picture user4387602 · Aug 18, 2017 · Viewed 8.9k times · Source

New to swagger and would like to know if there is a way to convert and existing jsonschema model to swagger specification.

Answer

jruizaranguren picture jruizaranguren · May 22, 2020

There is no direct translation between json-schema to swagger / openapi specifications.

From the openapi 2.0 documentation:

The files describing the RESTful API in accordance with the Swagger specification are represented as JSON objects and conform to the JSON standards. YAML, being a superset of JSON, can be used as well to represent a Swagger specification file.

And after:

An OpenAPI document MAY be made up of a single document or be divided into multiple, connected parts at the discretion of the user. In the latter case, $ref fields MUST be used in the specification to reference those parts as follows from the JSON Schema definitions.

Thus, swagger uses json-schema, but it adds also a bunch of new sections and rules.

But, if you craft carefuly your json-schema, such as having all your objects defined in a definitions tag, you can directly copy or nest that as part of the swagger specification.

You can also use "$ref" to point to objects defined in external json-schema files, no needing for any conversion.