【问题标题】:Why OpenAPI does not define '$ref' as allowed property?为什么 OpenAPI 没有将 '$ref' 定义为允许的属性?
【发布时间】:2018-04-18 12:50:25
【问题描述】:

draft-07 相比,它定义:

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

目前我正在尝试为openapi 编写验证器。但验证失败,因为 openapi schema(是的,它是来自 google apis 的架构)没有将 $ref 定义为 schema 的允许属性。

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

【问题讨论】:

    标签: openapi json-schema-validator


    【解决方案1】:

    $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 somethingreference (example):

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


    顺便说一句,官方 OpenAPI 规范存储库中目前有两种不同的 OAS3 JSON 模式草案。请随意尝试它们,并在相应的讨论中提供您的反馈。

    【讨论】:

    • 我发现我问的是不同的事情:不是我想要的。为什么 $ref 不是 JSON Schema draft-04 的一部分?有哪些建议如何在针对自身验证 draft-04 时检查 $ref
    • @EugenKonkov 我建议在JSON Schema repository 中提出这些问题 - 你可能会在那里得到最好的答案。
    猜你喜欢
    • 1970-01-01
    • 2012-01-27
    • 2011-04-26
    • 1970-01-01
    • 2012-09-20
    • 2021-11-25
    • 2018-12-26
    • 2020-02-15
    • 1970-01-01
    相关资源
    最近更新 更多