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

Đang mở
#442 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
52/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
backend-api-design

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
56
Fork
55
Merge trung bình
5 ngày 22 giờ
Pull request đã merge (30 ngày)
4

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của python-jsonschema/referencing

Tất cả issue của python-jsonschema/referencing

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.