Swagger Schema:oneOf、anyOf、allOf 同时有效? [英] Swagger Schema: oneOf, anyOf, allOf valid at the same time?

查看:249
本文介绍了Swagger Schema:oneOf、anyOf、allOf 同时有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在阅读 Swagger 规范的 Schema 定义中更高级的验证器:

I'm just reading through the more advanced validators in the Schema definition of the Swagger specification:

{
   "Schema":{
      "type":"object",
      "properties":{
         "title":{
            "type":"string"
         },
         "multipleOf":{
            "type":"number",
            "minimum":0,
            "exclusiveMinimum":true
         },
         "maximum":{
            "type":"number"
         },
         "exclusiveMaximum":{
            "type":"boolean",
            "default":false
         },
         "minimum":{
            "type":"number"
         },
         "exclusiveMinimum":{
            "type":"boolean",
            "default":false
         },
         "maxLength":{
            "type":"integer",
            "minimum":0
         },
         "minLength":{
            "type":"integer",
            "minimum":0,
            "default":0
         },
         "pattern":{
            "type":"string",
            "format":"regex"
         },
         "maxItems":{
            "type":"integer",
            "minimum":0
         },
         "minItems":{
            "type":"integer",
            "minimum":0,
            "default":0
         },
         "uniqueItems":{
            "type":"boolean",
            "default":false
         },
         "maxProperties":{
            "type":"integer",
            "minimum":0
         },
         "minProperties":{
            "type":"integer",
            "minimum":0,
            "default":0
         },
         "required":{
            "type":"array",
            "items":{
               "type":"string"
            },
            "minItems":1,
            "uniqueItems":true
         },
         "enum":{
            "type":"array",
            "items":{

            },
            "minItems":1,
            "uniqueItems":true
         },
         "type":{
            "type":"string",
            "enum":[
               "array",
               "boolean",
               "integer",
               "number",
               "object",
               "string"
            ]
         },
         "not":{
            "oneOf":[
               {
                  "$ref":"#/definitions/Schema"
               },
               {
                  "$ref":"#/definitions/Reference"
               }
            ]
         },
         "allOf":{
            "type":"array",
            "items":{
               "oneOf":[
                  {
                     "$ref":"#/definitions/Schema"
                  },
                  {
                     "$ref":"#/definitions/Reference"
                  }
               ]
            }
         },
         "oneOf":{
            "type":"array",
            "items":{
               "oneOf":[
                  {
                     "$ref":"#/definitions/Schema"
                  },
                  {
                     "$ref":"#/definitions/Reference"
                  }
               ]
            }
         },
         "anyOf":{
            "type":"array",
            "items":{
               "oneOf":[
                  {
                     "$ref":"#/definitions/Schema"
                  },
                  {
                     "$ref":"#/definitions/Reference"
                  }
               ]
            }
         },
         "items":{
            "oneOf":[
               {
                  "$ref":"#/definitions/Schema"
               },
               {
                  "$ref":"#/definitions/Reference"
               }
            ]
         },
         "properties":{
            "type":"object",
            "additionalProperties":{
               "oneOf":[
                  {
                     "$ref":"#/definitions/Schema"
                  },
                  {
                     "$ref":"#/definitions/Reference"
                  }
               ]
            }
         },
         "additionalProperties":{
            "oneOf":[
               {
                  "$ref":"#/definitions/Schema"
               },
               {
                  "$ref":"#/definitions/Reference"
               },
               {
                  "type":"boolean"
               }
            ],
            "default":true
         },
         "description":{
            "type":"string"
         },
         "format":{
            "type":"string"
         },
         "default":{

         },
         "nullable":{
            "type":"boolean",
            "default":false
         },
         "discriminator":{
            "$ref":"#/definitions/Discriminator"
         },
         "readOnly":{
            "type":"boolean",
            "default":false
         },
         "writeOnly":{
            "type":"boolean",
            "default":false
         },
         "example":{

         },
         "externalDocs":{
            "$ref":"#/definitions/ExternalDocumentation"
         },
         "deprecated":{
            "type":"boolean",
            "default":false
         },
         "xml":{
            "$ref":"#/definitions/XML"
         }
      },
      "patternProperties":{
         "^x-":{

         }
      },
      "additionalProperties":false
   }
}

我正在考虑的是 anyOf、allOf、oneOf 和 not 关键字的组合.我有两个问题.

The thing that I am thinking about is combinations of the anyOf, allOf, oneOf and not keywords. I have two questions.

第一个问题是:它们可以结合使用吗"?像这样:

The first question is: "can they be used in conjunction"? Like so:

{
   "allOf" : [
      {
         "minItems" : 0
      },
      {
         "maxItems" : 10
      }
   ],
   "anyOf" : [
      {
         "type" : "array",
         "items" : {
            "type" : "string"
         }
      },
      {
         "type" : "array",
         "items" : {
            "type" : "integer"
         }
      }
   ]
}

这个例子当然是不必要的复杂.但它有效吗?或者你可以只使用一个修饰符而不使用其他修饰符?

This example is, of course, needlessly complicated. But is it valid? Or can you only use one modifier but not the others?

第二个问题是,谁能给我指出一个真实世界的例子,其中这些运算符中的一个被结合使用?

The second question is, can anybody point me to a real-world example where one of these operators have been used in conjunction?

推荐答案

我正在考虑的是 anyOf、allOf、oneOf 和 not 关键字的组合.我有两个问题.

The thing that I am thinking about is combinations of the anyOf, allOf, oneOf and not keywords. I have two questions.

第一个问题是:它们可以结合使用吗"?

The first question is: "can they be used in conjunction"?

是的,allOfanyOfoneOfnot 可以结合使用.OpenAPI 规范在这里遵循 JSON Schema 的规则,在 JSON Schema 中,相邻关键字作为隐式 allOf (来源).所以你的例子相当于:

Yes, allOf, anyOf, oneOf and not can be used in conjunction. OpenAPI Specification follows the rules of JSON Schema here, and in JSON Schema adjacent keywords work as branches of an implicit allOf (source). So your example is equivalent to:

{
  "allOf": [
    {
      "allOf": [
        {
          "minItems": 0
        },
        {
          "maxItems": 10
        }
      ]
    },
    {
      "anyOf": [
        {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        {
          "type": "array",
          "items": {
            "type": "integer"
          }
        }
      ]
    }
  ]
}

也就是说,这个例子太复杂了,可以简化为:

That said, this example is too complex and can be simplified into:

{
  "minItems": 0,
  "maxItems": 10,
  "type": "array",
  "items": {
    "oneOf": [
      {
        "type": "string"
      },
      {
        "type": "integer"
      }
    ]
  }
}

这篇关于Swagger Schema:oneOf、anyOf、allOf 同时有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆