首页 > 解决方案 > 基本 URL 如何使用 JSON Schema 更改纯名称片段解析?

问题描述

摘自http://json-schema.org/draft-07/json-schema-core.html#rfc.section.8.2.4

{
    "$id": "http://example.com/root.json",
    "definitions": {
        "A": { "$id": "#foo" },
        "B": {
            "$id": "other.json",
            "definitions": {
                "X": { "$id": "#bar" },
                "Y": { "$id": "t/inner.json" }
            }
        },
        "C": {
            "$id": "urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f"
        }
    }
}

以下 URI 编码的 JSON 指针 [RFC6901](相对于根模式)中的模式具有以下基本 URI,并且可以根据上面的第 5 节由任何列出的 URI 识别:

...
#/definitions/B/definitions/X
        http://example.com/other.json#bar
        http://example.com/other.json#/definitions/X
        http://example.com/root.json#/definitions/B/definitions/X
...

为什么http://example.com/root.json#bar不是#/definitions/B/definitions/X 的有效基本 URI?

标签: jsonschema

解决方案


http://example.com/root.json#bar将无法使用给定的架构解析。

把它想象成 HTML 中的锚点。 $id就像定义一个新页面一样,因此 的位置#/definitions/B位于“页面”下$other.json,因此http://example.com/other.json#bar在您包含的列表中与#/definitions/B/definitions/X.

如果您创建一个位于 的 HTML 页面http://example.com/root.json,并尝试单击位于 的链接#bar,它将找不到 # 位置,因为它在other.json页面上。

如果其中任何一个没有意义或令人困惑,请告诉我。


推荐阅读