How to specify a property as null or a reference?

后端 未结 2 1886
心在旅途
心在旅途 2021-02-05 02:41

I have a json document in which a part can be either null or a subobject, like this:

[{
    \"owner\":null    
},
{
    \"owner\":{
        \"id\":1
    }   
}]
         


        
2条回答
  •  臣服心动
    2021-02-05 03:15

    What you've posted should work, if you remove the "type":"object" from the definition.

    However, a neater, more explicit way to specify alternatives is to use oneOf. You can keep your "id" definition untouched, and just use:

        "owner":{
            "oneOf": [
                {"type": "null"},
                {"$ref":"#/definitions/id"}
            ]
        }
    

提交回复
热议问题