`Resolver.lookup` always resolves a `$dynamicAnchor` dynamically, so a `$ref` to one behaves like a `$dynamicRef`

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

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

評価

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

調査の方向性

Start with Resolver.lookup and lookup_recursive_ref, then trace how jsonschema calls lookup for $ref and $dynamicRef. Use the supplied Draft 2020-12 reproduction to check the behavior. Done means $ref returns the static inner schema while $dynamicRef retains dynamic resolution.

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

説明

Resolver.lookup resolves a fragment that names a $dynamicAnchor through the dynamic scope, and a caller cannot ask for the static target instead. In Draft 2020-12 only $dynamicRef resolves that way; a $ref to the same anchor is static. jsonschema calls lookup for both keywords, so it applies the wrong schema for such a $ref whenever an outer resource has a dynamic anchor of the same name.

Seen with referencing 0.37.0 and with main at 84089b9.

Reproduction

from referencing import Registry
from referencing.jsonschema import DRAFT202012

schema = {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://example.com/root",
    "$ref": "inner#item",
    "$defs": {
        "outer": {"$dynamicAnchor": "item", "type": "null"},
        "inner": {"$id": "inner", "$dynamicAnchor": "item", "type": "string"},
    },
}
registry = DRAFT202012.create_resource(schema) @ Registry()
resolver = registry.resolver(base_uri="https://example.com/root")
print(resolver.lookup("inner#item").contents)
Result
Expected for a $ref {'$id': 'inner', '$dynamicAnchor': 'item', 'type': 'string'}
Actual ❌ Incorrect: {'$dynamicAnchor': 'item', 'type': 'null'}

https://example.com/inner#item is the inner schema, but lookup returns outer, the root resource's dynamic anchor of that name. That is right for a $dynamicRef and wrong for a $ref. From a resolver with an empty dynamic scope, registry.resolver().lookup("https://example.com/inner#item"), the same reference does return inner.

The visible effect is in jsonschema 4.26.0: Draft202012Validator(schema) rejects "a" and accepts null, the reverse of what the schema says.

Why a $ref should be static

Core §8.2.3.1 on $ref:

This resolution is safe to perform on schema load, as the process of evaluating an instance cannot change how the reference resolves.

§8.2.3.2 on $dynamicRef:

Both the extension point and the runtime-determined extension target are defined with "$dynamicAnchor", and only exhibit runtime dynamic behavior when referenced with "$dynamicRef".

What a fix needs

A way to look up a dynamic anchor statically here, and jsonschema using it for $ref. $recursiveRef is already split like that, with lookup_recursive_ref for the dynamic case.

Other implementations

Implementation $ref in the schema above
Python jsonschema 4.26.0 with referencing 0.37.0 ❌ Incorrect: "a" invalid, null valid
@hyperjump/json-schema 1.18.1 (JavaScript) ✅ Correct: "a" valid, null invalid
boon 0.6.1 (Rust) ✅ Correct: "a" valid, null invalid
ajv 8.20.0 (JavaScript) ✅ Correct: "a" valid, null invalid
Rust jsonschema crate 0.61.2 ❌ Incorrect: "a" invalid, null valid

The Rust crate has the same behaviour, reported as Stranger6667/jsonschema#1923.

The JSON Schema Test Suite does not catch this: its one case with a $ref to a $dynamicAnchor has a single anchor of that name, so both readings pass it. I have proposed a case that tells them apart in json-schema-org/JSON-Schema-Test-Suite#1279.

主要言語
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 を短くまとめたダイジェスト。