How to specify a property as null or a reference?

后端 未结 2 1891
心在旅途
心在旅途 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"}
            ]
        }
    
    0 讨论(0)
  • 2021-02-05 03:32

    nullable field will be supported in OpenApi (aka Swagger) Specification v3.0.0

    So with this new spec your definition would look like:

    "properties":{
        "owner":{
            "nullable": true,
             ...
        }
    },
    
    0 讨论(0)
提交回复
热议问题