fix: resolve $dynamicRef for anonymous root schemas#367
Open
aqeelat wants to merge 2 commits into
Open
Conversation
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
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a root schema has no
$idand uses$refto pull in a template containing$dynamicRef, the$dynamicRefignores any$dynamicAnchoroverrides defined in the root schema's$defs. It falls back to the template's own$dynamicAnchorinstead.Filed from python-jsonschema/jsonschema#1497.
Root Cause
_evolve()in_core.pyuses a truthiness guardif self._base_uri and ...that treats""(the base URI for anonymous roots) as falsy. This prevents the anonymous root from being pushed onto the_previousstack, so it never appears indynamic_scope().Fix
Add an explicit lookup for the anonymous root (
"") inDynamicAnchor.resolve()after iteratingdynamic_scope(). This ensures$dynamicAnchoroverrides on root schemas without$idare found.Test
Added
test_dynamic_ref_with_anonymous_root_schemathat reproduces the exact scenario from python-jsonschema/jsonschema#1497: an anonymous root with a$dynamicAnchoroverride that$dynamicRefshould resolve to.Fixes #366