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

Abierto
#442 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
52/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Python
Estrellas
56
Forks
55
Merge medio
5 d 22 h
PR fusionados (30 d)
4

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de python-jsonschema/referencing

Todos los issues de python-jsonschema/referencing

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.