$ref resolution in a $dynamicRef target ($dynamicAnchor) happening from base URI instead of target

オープン
#445 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
30/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
json, python
領域
tooling

調査の方向性

Run repro.py against the two schemas in base.schema.json and extension.schema.json to confirm the PointerToNowhere failure, then trace how referencing resolves the $ref: "#/$defs/B" written inside the extension's $dynamicAnchor target. The fix is to resolve that $ref against the resource the target is written in (the extension's $id) rather than the base schema, so "done" means the repro prints True then False without changing other resolution behaviour. This needs familiarity with JSON Schema 2020-12 dynamic scope and the resolver internals, so read the surrounding resolution code and existing dynamicRef tests before editing.

索引モデルが issue の本文から書いたものです。

説明

referencing 0.37.0 resolves $refs inside a $dynamicRef target against the referring schema's base URI instead of the target's resource, contrary to JSON Schema 2020-12 §8.2.

8.2.3.1 "The value of the "$ref" keyword MUST be a string which is a URI-Reference. Resolved against the current URI base, it produces the URI of the schema to apply"

  1. The base URI for a $ref comes from the schema resource the $ref is written in, not from the path validation took to get there.
  2. The slot definition with "$ref": "#/$defs/B" is written inside the extension, so its base URI is the extension's $id.
  3. $dynamicRef decides which schema gets applied: the extension's definition. It doesn't change where that definition is written.
  4. So #/$defs/B should resolve inside the extension. referencing resolves it inside the base schema.

To reproduce:
Folder Layout

repro/
  base.schema.json
  extension.schema.json
  repro.py

base.schema.json:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/schemas/base.schema.json",
  "type": "object",
  "allOf": [{ "$dynamicRef": "#ext" }],
  "properties": {
    "a": { "type": "integer" }
  },
  "unevaluatedProperties": false,
  "$defs": {
    "ext": { "$dynamicAnchor": "ext" }
  }
}

extension.schema.json:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/schemas/extension.schema.json",
  "$ref": "base.schema.json",
  "$defs": {
    "ext": {
      "$dynamicAnchor": "ext",
      "properties": {
        "b": { "$ref": "#/$defs/B" }
      }
    },
    "B": { "type": "string" }
  }
}

repro.py:

import json
from pathlib import Path

from jsonschema import Draft202012Validator
from referencing import Registry, Resource

here = Path(__file__).parent
schemas = [json.loads((here / name).read_text()) for name in ("base.schema.json", "extension.schema.json")]
registry = Registry().with_resources((s["$id"], Resource.from_contents(s)) for s in schemas)

validator = Draft202012Validator(
    {"$ref": "https://example.com/schemas/extension.schema.json"}, registry=registry
)
print(validator.is_valid({"a": 1, "b": "text"}))  # expected: True
print(validator.is_valid({"a": 1, "b": 3}))       # expected: False

Expected: True, then False
Actual (jsonschema 4.26.0 and referencing 0.37.0):

jsonschema.exceptions._WrappedReferencingError: PointerToNowhere: '/$defs/B' does not exist within
{'$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'https://example.com/schemas/base.schema.json', ...}

主要言語
Python
スター
56
フォーク
55
平均マージ
5日 22時間
マージ済み PR(30日)
4

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python-jsonschema/referencing のほかの issue

python-jsonschema/referencing の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。