python-jsonschema / python-jsonschema/jsonschema

$dynamicRef doesn't find $dynamicAnchor overrides in root schemas without $id

Open
#1,497 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5k
Forks
671
Avg merge
1d 1h
Merged PRs (30d)
10

Description

$dynamicRef doesn't find $dynamicAnchor overrides in root schemas without $id

When a root schema has no $id and uses $ref to pull in a template containing $dynamicRef, the $dynamicRef ignores any $dynamicAnchor overrides defined in the root schema's $defs. It falls back to the template's own $dynamicAnchor instead.

Minimal reproducer

from jsonschema import Draft202012Validator
from referencing import Registry, Resource
from referencing.jsonschema import DRAFT202012

template = {
    "$id": "https://example.com/PaginatedTemplate",
    "$defs": {"itemType": {"$dynamicAnchor": "itemType", "not": {}}},
    "type": "object",
    "required": ["items"],
    "properties": {"items": {"type": "array", "items": {"$dynamicRef": "#itemType"}}}
}

schema = {
    "$defs": {"itemType": {"$dynamicAnchor": "itemType", "type": "string"}},
    "$ref": "https://example.com/PaginatedTemplate"
}

registry = Registry().with_resource(
    "https://example.com/PaginatedTemplate",
    Resource.from_contents(template, default_specification=DRAFT202012)
)

# Should pass — "hello" matches the override (type: string)
# Actually fails — $dynamicRef resolves to the template's not: {} instead
v = Draft202012Validator(schema, registry=registry)
assert not list(v.iter_errors({"items": ["hello"]}))  # fails

Adding $id to the root schema makes it work:

schema["$id"] = "https://example.com/Bound"
# ... then register and validate — passes

Workaround

Add a synthetic $id to the root schema before creating the validator.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Draft202012Validator setup and the Registry/Resource registration in the reproducer, then trace how the root schema without an $id is resolved through $ref and how $dynamicRef finds its anchor. Reproduce the iter_errors failure, and consider the issue done when the root $defs override is honored without adding a synthetic $id.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.