wework/json-schema-to-openapi-schema

Cannot convert type null with $ref

janjagusch opened this issue · 0 comments

I am trying to convert a json schema that should either be null or a $ref to another object. In this case the reference is just a simple string but this could be arbitrarily complicated.

The design of the json schema follows the advice given here:

Input

{
    "anyOf": [
        {
            "$ref": "#/my_object"
        },
        {
            "type": "null"
        }
    ],
    "my_object": {
        "type": "string"
    }
}

I expected the output to be somewhat similar to this:

Expected Output

{
  "nullable": true,
  "$ref": "#/my_object",
  "my_object": {
    "type": "string"
  }
}

However, it actually caused the package to crash:

Error

Error
    at Object.<anonymous> ([...]/json-schema-to-openapi-schema/index.js:8:30)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> ([...]/json-schema-to-openapi/index.js:3:19)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10) {
  name: 'InvalidTypeError',
  message: 'Type "null" is not a valid type'
}

I believe this issue is similar to #21. However, in that case you could easily adjust the json schema using the "type": [] syntax. Here it is different. I guess the only alternative would be to make all referenced objects nullable, which I would like to avoid.

I believe this would be a great addition for converting rather complicated schemata. :)