2

I'm looking for a way to convert a schema defined inside a swagger to a JSON-Schema.

For example, in my swagger i have :

"definitions" : {
    "response" : {
        "properties" : {
            "responseContent" : {
                "$ref" : "#/definitions/objectResponse"
            }
        }
    },

    "objectResponse" : {
        "description" : "response",
        "properties" : {
            "idResponse" : {
                "type"        : "string",
                "description" : "Response",
                "minLength"   : 0,
                "maxLength"   : 255
            }
        }
    }
}

I want :

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {},
  "id": "http://example.com/example.json",
  "properties": {
    "responseContent": {
      "id": "/properties/responseContent",
      "properties": {
        "idResponse": {
          "id": "/properties/responseContent/properties/idResponse",
          "type": "string"
        }
      },
      "type": "object"
    }
  },
  "type": "object"
}

At the moment, i found a solution :

  • generate a response based on the swagger (e.g. with apimatic.io)
  • convert the response with jsonschema.net

But with this process, i lose restrictions, and i depend on external tools.

Thanks for your help.

PS : I'm kind of new to the API world, don't hesitate to explain deeply I'm curious

PIer

marsaldev
  • 3,338
  • 2
  • 16
  • 27
PIer
  • 41
  • 1
  • 4
  • 1
    Looking for something like [this](https://github.com/garethr/openapi2jsonschema)? (Never used it before, I just googled.) – Tom Lord Jul 27 '17 at 11:37
  • *"schema defined inside a swagger"* -- To clarify, Swagger follows the [Open-API Specification](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schema). All you need to do is convert between the two JSON formats. – Tom Lord Jul 27 '17 at 11:39
  • 1
    Related: [How to generate JSON-Schema from Swagger API Declaration](https://stackoverflow.com/q/24118243/113116) – Helen Jul 27 '17 at 13:29

0 Answers0