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

Aperta
#442 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
52/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
56
Fork
55
Merge medio
5g 22h
PR unite (30g)
4

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python-jsonschema/referencing

Tutte le issue di python-jsonschema/referencing

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.