88

I have Swagger API Declaration for services using Swagger v 1.2.

My original feeling about Swagger was that it is very close to JSON Schema (Draft 3 and lately Draft 4) and it shall be relatively easy to generate JSON Schema for request and response objects.

However, while part of the Swagger reuses JSON Schema structures, it turned out that it uses only a subset of features, and it also introduces its own inheritance in Models (using subTypes and discriminator).

Question: Is there any existing project or piece of code which can generate usable JSON Schema from Swagger API Declaration?

Optimally JSON Schema Draft 4 and using Python (but I will be happy to find anything).

Pang
  • 9,564
  • 146
  • 81
  • 122
Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98

8 Answers8

88

After longer fight with using Swagger for specifying REST API and reusing it in related test suites, I will share my own experience with it (answering my own question).

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.
  • thinking of XML, Swagger supports only canonical representation of subset of JSON structures provided by JSON Schema Draft 4.

In other words:

  • Swagger (1.2 and 2.0) does not support usage of many JSON structures, which are valid in terms of JSON Schema Draft 4 (the same applies to Draft 3).
  • Swagger does not support general XML data structures, only very restricted structures are allowed.

In practice, you cannot start with designing your data in JSON or XML, with Swagger you have to start and end with Swagger.

Getting JSON Schema is theoretically possible, but not easy

I have spent some time coding a library, which would take Swagger API Specification and create JSON Schema Draft 4. I gave up for couple of reasons:

  • it was not easy at all
  • got disappointed finding, that I can use only subset of what JSON Schema provides. We had some JSON payload already proposed and had to start modifying it just to fit what Swagger specification framework allows.

Apart from having really nice looking UI for showing and testing the API (yes, everybody agrees, it is visually very pleasing), I have found it weird, that a specification framework is not allowing us to use what we want, but adds unexpected restrictions to our design.

If you want full JSON or XML Schema support, use RAML

Researching other API specification frameworks, I have found RAML. As it is built from ground up by supporting any JSON Schema Draft 3/4 or W3C XML Schema 1.0 data structures, the experience was excellent - having structure of my payload designed, I was able authoring API specification very quickly and following validation of real requests and responses against defined schemas was very easy, as the schemas are essentials components of the specification without adding any restrictions on them.

RAML was at version 0.8 that time (version 1.0 was not released yet).

Correcting the question leads to real solution

Good question makes half of the solution. My question was wrong as it missed fulfilling my real expectations. Corrected question would be:

What specification framework and technique to use, to specify REST API using payload defined by arbitrary JSON Schema Draft 4 or W3C XML Schema 1.0.

My answer to such a question would be:

  1. Design your payload in JSON Schema Draft 4 or W3C XML Schema
  2. Describe your REST API by means of RAML (v0.8 at the moment).

There might be other specification frameworks usable, but Swagger (neither v1.2 nor v2.0) is definitely not the case. Apart from providing really a lot of features (code generation, very nice looking documentation of the API and much more), it simply fails in providing solution to the updated question stated above.

automatix
  • 14,018
  • 26
  • 105
  • 230
Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
  • There are some Swagger to RAML converters out there. @jan-vlcinsky do you think this could work for "simple" schemas? `swagger` -> `RAML` -> `JSON Schema` – webwurst Dec 02 '15 at 19:04
  • @webwurst sounds promising. Are you aware of any tool, which converts RAML to JSON Schema? Or does the convertion to RAML already builds the JSON Schema for the elements? Anyway, my main problems with Swagger was false expectation, that in Swagger I can express anything what JSON Schema allows and this was not true. – Jan Vlcinsky Dec 02 '15 at 20:04
  • The only problem is that the community around RAML is still not popular enough. And just like what you said, if Swagger allows full JSON schema, I can remove dozen of code in pyswagger by replacing them with better python parser. – mission.liao May 07 '16 at 00:26
  • Let's say your data is modeled in JSON (not XML) schema. What are the main advantages of using RAML over JSON Hyper-Schema to define your API? It seems like the list of missing features would form a starting point for JSON Hyper-Schema draft-5 (assuming there's a community behind that proposal). – kerlyn Oct 14 '16 at 18:28
16

There is a python tool to do the same by the name openapi2jsonschema. You can simply install it using pip.

The readme for openapi2 shows the simplest way to use it:

openapi2jsonschema https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json

Hope this helps.

Akash Masand
  • 1,441
  • 14
  • 30
7

I just wrote a tool pyswagger seems fit your need.

It loads Swagger API declaration, and able to convert python object to/from Swagger primitives. Also provide a set of client implementations (including requests & tornado.httpclient.AsyncHTTPClient) that able to make request to Swagger-enabled service directly.

This tool tends to solve the first problem I encountered when adapting Swagger in python, and is still pretty new now. Welcome for any suggestion.

mission.liao
  • 328
  • 5
  • 12
  • @missionliao First impression is very good. Within few weeks I shall come to investigate it in more detail, publish my application (currently named SwaggerProxy) and we may join our effort. Some of the design decision visible in your solution are very similar to what I have done too, this is promising signal. – Jan Vlcinsky Sep 02 '14 at 13:17
1

I've had some success doing it like this:

swagger.yaml -> proto -> jsonschema

I used openapi2proto to generate proto files from Swagger yaml, then protoc-gen-jsonschema to generate the JSONSchemas from that. It's good enough to get a typed JSONSchema, but proto3 doesn't support "required" types so you miss out on this.

Chrusty
  • 11
  • 1
0

Install OpenApi to Jsonschema extractor:

Open terminal - do the following commands

sudo yum install python-pip
pip install openapi2jsonschema
  • download the openApi yaml file to a folder

  • cd to the downloaded folder and then run this command

openapi2jsonschema --strict <openapi yaml filename>
bad_coder
  • 11,289
  • 20
  • 44
  • 72
sat
  • 11
0

I've written one recursive function for creating json schema from Swagger definition.

For example, consider the swagger specification petstore-minimal

The definition Pet is converted into the below json-schema

{
  "description": null,
  "type": "object",
  "properties": {
    "name": {
      "description": null,
      "type": "string"
    },
    "id": {
      "format": "int64",
      "description": null,
      "type": "integer"
    },
    "tag": {
      "description": null,
      "type": "string"
    }
  }
}

Ofcourse, this is a very minimal json schema, but can be achieved much more with the function that I wrote. Let me explain the way I did this, I used the following maven dependency to fetch the definitions from swagger specification

<dependency>
        <groupId>io.swagger.parser.v3</groupId>
        <artifactId>swagger-parser</artifactId>
        <version>2.1.2</version>
    </dependency>

To create the json schema, I used the below maven dependency

<dependency>
        <groupId>net.minidev</groupId>
        <artifactId>json-smart</artifactId>
        <version>2.4.7</version>
    </dependency>

Now to the coding part, the input is the location of the swagger spec and also the definition name which we need to convert to json schema

public static void main(String[] args) {
    String jsonSchema = SwaggerUtil.generateJsonSchemaFromSwaggerSpec("path to swagger spec", "Pet");
    System.out.println(jsonSchema);
}

Now, we need to process the swagger definition passed as the input and recursively process the properties of the definition

public static String generateJsonSchemaFromSwaggerSpec(String swaggerPath, String fieldName){

    Swagger swagger = new SwaggerParser().read(swaggerPath);
    Map<String, Model> definitions = swagger.getDefinitions();
    Model schemaGenerationDefinition = definitions.get(fieldName);
    Map<String, Property> propertyMap = schemaGenerationDefinition.getProperties();

    Map<String, JsonProperty> customJsonPropertyMap = new HashMap<>();
    propertyMap.forEach((propertyName, property) -> {
        JsonProperty jsonProperty = processSwaggerProperties(propertyName, property, definitions);
        customJsonPropertyMap.put(propertyName, jsonProperty);
    });
    JsonObjectProperty objectProperty = new JsonObjectProperty(customJsonPropertyMap, schemaGenerationDefinition.getDescription());
    JSONObject generatedObject = objectProperty.toJsonObject();
    String jsonSchema = generatedObject.toJSONString();
    return jsonSchema;
}

private static JsonProperty processReference(String referenceName, String type, Map<String, Model> definitions){

    Model model = definitions.get(referenceName);
    Map<String, Property> propertyMap = model.getProperties();
    Map<String, JsonProperty> jsonPropertyMap = new HashMap<>();
    propertyMap.forEach((propertyName, property) -> {
        JsonProperty jsonProperty = processSwaggerProperties(propertyName, property, definitions);
        jsonPropertyMap.put(propertyName, jsonProperty);
    });
    if (type.equalsIgnoreCase("array")){
        JsonArrayProperty jsonProperty = new JsonArrayProperty(model.getDescription());
        jsonProperty.loadPropertiesFromMap(jsonPropertyMap);
        return jsonProperty;
    }else{
        JsonObjectProperty objectProperty = new JsonObjectProperty(jsonPropertyMap, model.getDescription());
        return objectProperty;
    }
}

private static JsonProperty processSwaggerProperties(String propertyName, Property property, Map<String, Model> propertyDefinitions){
    String definitionRefPath = "";
    String type = "";
    JsonProperty jsonProperty = null;
    if (property.getType().equalsIgnoreCase("ref")){
        definitionRefPath = ((RefProperty) property).getOriginalRef();
        type = "object";
    }else if (property.getType().equalsIgnoreCase("array")){
        type = "array";
        Property childProperty = ((ArrayProperty) property).getItems();
        if (childProperty instanceof RefProperty){
            RefProperty refProperty = (RefProperty) ((ArrayProperty) property).getItems();
            definitionRefPath = refProperty.getOriginalRef();
        }else{
            JsonArrayProperty arrayProperty = new JsonArrayProperty(property.getDescription());
            arrayProperty.loadChildProperty(childProperty);
            return arrayProperty;
        }
    }else{
        jsonProperty = PropertyFactory.createJsonProperty(property);
        return jsonProperty;
    }
    String[] splitResult = definitionRefPath.split("/");
    if (splitResult.length == 3) {
        String propertyPath = splitResult[2];
        System.out.println(propertyPath);
        jsonProperty = processReference(propertyPath, type, propertyDefinitions);
    }
    return jsonProperty;
}

So for creating the json schema, I created my own custom json schema classes. that is for each of the json schema data types.. Also wrote from factory class to create the required json type

    public class PropertyFactory {


    public static JsonProperty createJsonProperty(Property property){
        JsonProperty jsonProperty = null;
        switch (property.getType()){
            case "number":
                jsonProperty = new JsonNumberProperty(property.getFormat(), property.getDescription());
                break;
            case "string":
                jsonProperty = new JsonStringProperty(property.getDescription());
                break;
            case "boolean":
                jsonProperty = new JsonBooleanProperty(property.getDescription());
                break;
            case "integer":
                jsonProperty = new JsonIntegerProperty(property.getFormat(), property.getDescription());
                if (property instanceof IntegerProperty){
                    IntegerProperty integerProperty = (IntegerProperty) property;
                    if (integerProperty.getMinimum() != null)
                        ((JsonIntegerProperty) jsonProperty).setMinimum(integerProperty.getMinimum());
                    if (integerProperty.getMaximum() != null)
                        ((JsonIntegerProperty) jsonProperty).setMaximum(integerProperty.getMaximum());
                }else if (property instanceof LongProperty){
                    LongProperty longProperty = (LongProperty) property;
                    if (longProperty.getMinimum() != null)
                        ((JsonIntegerProperty) jsonProperty).setMinimum(longProperty.getMinimum());
                    if (longProperty.getMaximum() != null)
                        ((JsonIntegerProperty) jsonProperty).setMaximum(longProperty.getMaximum());
                }
                break;
            default:
                System.out.println("Unhandled type");
        }

        return jsonProperty;
    }
}

Below are the abstractions I created for each of the json datatypes


    public abstract class JsonProperty {

    protected String type;

    protected JSONArray required;

    protected String description;


    protected JsonProperty(String type, String description){
        this.type = type;
        this.description = description;
    }

    protected abstract JSONObject toJsonObject();

}


    public class JsonArrayProperty extends JsonProperty{


    private JsonProperty items;


    public JsonArrayProperty(String description){
        super("array", description);
    }

    @Override
    protected JSONObject toJsonObject() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", this.type);
        jsonObject.put("description", this.description);
        jsonObject.put("items", this.items.toJsonObject());
        return jsonObject;
    }

    public void loadPropertiesFromMap(Map<String, JsonProperty> propertyMap){
        this.items = new JsonObjectProperty(propertyMap, this.description);
    }

    public void loadChildProperty(Property childProperty){
        this.items = PropertyFactory.createJsonProperty(childProperty);
    }}

public class JsonObjectProperty extends JsonProperty{

    private Map<String, JsonProperty> properties;


    public JsonObjectProperty(Map<String, JsonProperty> properties, String description){
        super("object", description);
        this.properties = properties;
    }

    @Override
    public JSONObject toJsonObject() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", this.type);
        jsonObject.put("description", this.description);
        JSONObject propertyObject = new JSONObject();
        this.properties.forEach((propertyName, jsonProperty) -> {
            if (jsonProperty != null) {
                JSONObject object = jsonProperty.toJsonObject();
                propertyObject.put(propertyName, object);
            }
        });
        jsonObject.put("properties", propertyObject);
        return jsonObject;
    }


    public Map<String, JsonProperty> getProperties() {
        return properties;
    }

    public void setProperties(Map<String, JsonProperty> properties) {
        this.properties = properties;
    }

}
public class JsonNumberProperty extends JsonProperty {

    protected String format;

    public JsonNumberProperty(String format, String description) {
        super("number", description);
        this.format = format;
    }

    public JsonNumberProperty(String type, String format, String description){
        super(type, description);
        this.format = format;
    }

    @Override
    protected JSONObject toJsonObject() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", this.type);
        jsonObject.put("description", this.description);
        if (this.format != null)
            jsonObject.put("format", this.format);
        return jsonObject;
    }


}

public class JsonIntegerProperty extends JsonNumberProperty{

    private String pattern;

    private BigDecimal minimum;

    private BigDecimal maximum;


    public JsonIntegerProperty(String format, String description) {
        super("integer", format, description);
    }

    @Override
    protected JSONObject toJsonObject() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", this.type);
        jsonObject.put("description", this.description);
        if (this.format != null)
            jsonObject.put("format", this.format);
        if (this.minimum != null)
            jsonObject.put("minimum", this.minimum);
        if (this.maximum != null)
            jsonObject.put("maximum", this.maximum);
        return jsonObject;
    }


    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public void setMinimum(BigDecimal minimum) {
        this.minimum = minimum;
    }

    public void setMaximum(BigDecimal maximum) {
        this.maximum = maximum;
    }
}
public class JsonStringProperty extends JsonProperty{

    public JsonStringProperty(String description) {
        super("string", description);
    }

    @Override
    protected JSONObject toJsonObject() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", this.type);
        jsonObject.put("description", this.description);
        return jsonObject;
    }
}

NOTE

This is a custom implementation that I did for my needs, if you come across any additional datatypes, you can simply create the type by extending the JsonProperty class and providing the toJsonObject implementation.

Happy Coding

emilpmp
  • 1,716
  • 17
  • 32
0

using Swagger UI for documentation the highlighted link returns json object for your api schema :

enter image description here

BNG016
  • 164
  • 1
  • 7
0

2023 Answer Update:

Open https://editor.swagger.io/

Paste your swagger.yaml file

Click Edit -> Convert and save as JSON as shown below.

Enjoy!

enter image description here

CollinsKe
  • 73
  • 8