json-schema-to-openapi-schema icon indicating copy to clipboard operation
json-schema-to-openapi-schema copied to clipboard

JSON Schema - Definitions

Open jasonterando opened this issue 4 years ago • 2 comments

Hi, does this routine support the definitions keyword as described in the JSON Schema documentation? Using json-schema-to-openapi-schema, converting a JSON Schema with a "definitions" property does not seem to handle nullable types properly (sending the individual interfaces to json-schema-to-openapi-schema works. Note that I am not using #refs.

Here is an example:

JSON Schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "Product": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "price": {
          "type": "number"
        },
        "rating": {
          "type": [
            "null",
            "number"
          ]
        }
      },
      "required": [
        "name",
        "price",
        "rating"
      ]
    },
    "ProductList": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "version": {
          "type": "string"
        },
        "products": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string"
              },
              "price": {
                "type": "number"
              },
              "rating": {
                "type": [
                  "null",
                  "number"
                ]
              }
            },
            "required": [
              "name",
              "price",
              "rating"
            ]
          }
        }
      },
      "required": [
        "name",
        "products",
        "version"
      ]
    }
  }
}

Results in OpenAPI JSON (note "null" types):

{
   "Product": {
      "type": "object",
      "properties": {
         "name": {
            "type": "string"
         },
         "price": {
            "type": "number"
         },
         "rating": {
            "type": [
               "null",
               "number"
            ]
         }
      },
      "required": [
         "name",
         "price",
         "rating"
      ]
   },
   "ProductList": {
      "type": "object",
      "properties": {
         "name": {
            "type": "string"
         },
         "version": {
            "type": "string"
         },
         "products": {
            "type": "array",
            "items": {
               "type": "object",
               "properties": {
                  "name": {
                     "type": "string"
                  },
                  "price": {
                     "type": "number"
                  },
                  "rating": {
                     "type": [
                        "null",
                        "number"
                     ]
                  }
               },
               "required": [
                  "name",
                  "price",
                  "rating"
               ]
            }
         }
      },
      "required": [
         "name",
         "products",
         "version"
      ]
   }
}

I was hoping to see something like this:

{
   "Product": {
      "type": "object",
      "properties": {
         "name": {
            "type": "string"
         },
         "price": {
            "type": "number"
         },
         "rating": {
            "type": "number",
            "nullable": true
         }
      },
      "required": [
         "name",
         "price",
         "rating"
      ]
   },
   "ProductList": {
      "type": "object",
      "properties": {
         "name": {
            "type": "string"
         },
         "version": {
            "type": "string"
         },
         "products": {
            "type": "array",
            "items": {
               "type": "object",
               "properties": {
                  "name": {
                     "type": "string"
                  },
                  "price": {
                     "type": "number"
                  },
                  "rating": {
                     "type": "number",
                     "nullable": true
                  }
               },
               "required": [
                  "name",
                  "price",
                  "rating"
               ]
            }
         }
      },
      "required": [
         "name",
         "products",
         "version"
      ]
   }
}

jasonterando avatar Dec 15 '20 15:12 jasonterando

Facing same issue, Please can someone update on if definitions keyword supported? @mikunn @philsturgeon

omkarnix avatar Oct 28 '21 11:10 omkarnix

This version hasn't been supported in years, and the new version is not being actively developed as OpenAPI v3.1 is now proper JSON Schema. Yaaaaay.

Anyhow, let's keep the discussion going over here, maybe you could help with a PR? https://github.com/openapi-contrib/json-schema-to-openapi-schema/issues/25

philsturgeon avatar Oct 28 '21 18:10 philsturgeon