Skip to content

fix: resolve $dynamicRef for anonymous root schemas#367

Open
aqeelat wants to merge 2 commits into
python-jsonschema:mainfrom
aqeelat:fix/dynamic-ref-anonymous-root
Open

fix: resolve $dynamicRef for anonymous root schemas#367
aqeelat wants to merge 2 commits into
python-jsonschema:mainfrom
aqeelat:fix/dynamic-ref-anonymous-root

Conversation

@aqeelat

@aqeelat aqeelat commented Jun 12, 2026

Copy link
Copy Markdown

Problem

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.

Filed from python-jsonschema/jsonschema#1497.

Root Cause

_evolve() in _core.py uses a truthiness guard if self._base_uri and ... that treats "" (the base URI for anonymous roots) as falsy. This prevents the anonymous root from being pushed onto the _previous stack, so it never appears in dynamic_scope().

Fix

Add an explicit lookup for the anonymous root ("") in DynamicAnchor.resolve() after iterating dynamic_scope(). This ensures $dynamicAnchor overrides on root schemas without $id are found.

Test

Added test_dynamic_ref_with_anonymous_root_schema that reproduces the exact scenario from python-jsonschema/jsonschema#1497: an anonymous root with a $dynamicAnchor override that $dynamicRef should resolve to.

Fixes #366

aqeelat and others added 2 commits June 12, 2026 17:31
When a root schema has no $id, its base URI is "" (empty string).
_evolve() uses a truthiness guard that treats "" as falsy, so the
anonymous root never appears in dynamic_scope(). This causes
$dynamicRef to miss $dynamicAnchor overrides defined in the root
schema's $defs, falling back to the template's own $dynamicAnchor.

Add an explicit lookup for the anonymous root ("") in the registry
after iterating dynamic_scope(), so dynamic anchors on root schemas
without $id are found.

Fixes python-jsonschema#366
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant