为什么OpenAPI没有定义' $ ref'作为允许的财产?

时间:2018-04-18 12:50:26

标签: openapi json-schema-validator

draft-07相比,它定义:

{
    "type": ["object", "boolean"],
    "properties": {
        ...
        "$ref": {
            "type": "string",
            "format": "uri-reference"
        },
    }
    ...
}

目前我正在尝试为openapi编写验证器。但验证失败是因为openapi schema(是的,它是来自Google API的模式)并未将$ref定义为schema的允许属性。

这是一个错字吗?关于如何检查$ref属性的建议是什么?

1 个答案:

答案 0 :(得分:2)

$ref是一个JSON参考。它不是schema定义的一部分,而是reference定义的一部分:

"reference": {
  "type": "object",
  "description": "A simple object to allow referencing other components in the specification, internally and externally.  The Reference Object is defined by JSON Reference and follows the same structure, behavior and rules.   For this specification, reference resolution is accomplished as defined by the JSON Reference specification and not by the JSON Schema specification.",
  "required": [
    "$ref"
  ],
  "additionalProperties": false,
  "properties": {
    "$ref": {
      "type": "string"
    }
  }
},

然后允许使用$ref的其他定义使用oneOf 某些内容referenceexample):

"schemaOrReference": {
  "oneOf": [
    {
      "$ref": "#/definitions/schema"
    },
    {
      "$ref": "#/definitions/reference"
    }
  ]
},


顺便说一下,官方OpenAPI规范库中目前有两个不同的OAS3 JSON Schema草案。请随意尝试,并在相应的讨论中提供反馈。