mikunn / openapi-schema-to-json-schema

Converts OpenAPI Schema Object to JSON Schema
76 stars 6 forks source link

Nullable not working with oneOf #31

Open kimchisus opened 5 years ago

kimchisus commented 5 years ago

openAPI ` nullable: true oneOf:

converts into

"oneOf": [ {"type": "number"}, {"type": "string"} ]

but type null doesn't exist.

BenMorel commented 5 years ago

Same thing with allOf:

{
    "nullable": true,
    "allOf": [
        {
            "$ref": "#/components/schemas/Deal"
        }
    ]
}

Converts to:

{
    "allOf": [
        {
            "$ref": "#/components/schemas/Deal"
        }
    ]
}

nullable is lost. @mikunn Do you plan to fix this? I can help but I'm not sure where to start.

BenMorel commented 5 years ago

I just fixed this in my PHP port of your library: https://github.com/BenMorel/openapi-schema-to-json-schema/commit/bb18eec80dcace69872010635f10ef511650e408

...if you want, in turn, to port the fix!

It turns any schema that contains nullable: true + oneOf/anyOf/allOf into a nested oneOf:

{
    "nullable": true,
    "allOf": [
        {
            "$ref": "#/components/schemas/Deal"
        }
    ]
}
{
    "oneOf": [
        {
            "type": "null",
        },
        {
            "allOf": [
                {
                    "$ref": "#/components/schemas/Deal"
                }
            ]
        }
    ],
    "$schema": "http://json-schema.org/draft-04/schema#"
}

Just tested this against a JSON Schema validator, and it works as expected.