wework / json-schema-to-openapi-schema

A little NodeJS package to convert JSON Schema to OpenAPI Schema Objects
217 stars 44 forks source link

JSON Schema - Definitions #29

Open jasonterando opened 3 years ago

jasonterando commented 3 years ago

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"
      ]
   }
}
omkarnix commented 3 years ago

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

philsturgeon commented 3 years ago

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