Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve $ref history. #1144

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion jsonschema/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(
schema_path=(),
parent=None,
type_checker=_unset,
refs=()
):
super().__init__(
message,
Expand All @@ -63,6 +64,7 @@ def __init__(
schema,
schema_path,
parent,
refs,
)
self.message = message
self.path = self.relative_path = deque(path)
Expand All @@ -74,6 +76,7 @@ def __init__(
self.instance = instance
self.schema = schema
self.parent = parent
self.refs = deque(refs)
self._type_checker = type_checker

for error in context:
Expand Down Expand Up @@ -156,7 +159,7 @@ def _set(self, type_checker=None, **kwargs):
def _contents(self):
attrs = (
"message", "cause", "context", "validator", "validator_value",
"path", "schema_path", "instance", "schema", "parent",
"path", "schema_path", "instance", "schema", "parent", "refs",
)
return dict((attr, getattr(self, attr)) for attr in attrs)

Expand Down
12 changes: 12 additions & 0 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,18 @@ def test_ref_sibling(self):
),
)

def test_ref_history(self):
schema = {
"$defs": {"foo": {"properties":{"prop":{"$ref":"#/$defs/bar"},},},
"bar": {"properties":{"a":{"type":"integer"},},},},
"$ref": "#/$defs/foo"
}
instance = {"prop": {"a":"1",}}

validator = validators._LATEST_VERSION(schema)
e, = list(validator.iter_errors(instance))
self.assertEqual(list(e.refs), ["#/$defs/foo", "#/$defs/bar"])


class MetaSchemaTestsMixin:
# TODO: These all belong upstream
Expand Down
4 changes: 4 additions & 0 deletions jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ def iter_errors(self, instance, _schema=None):
schema=_schema,
type_checker=self.TYPE_CHECKER,
)
if k == "$ref":
error.refs.appendleft(v)
if k not in {"if", "$ref"}:
error.schema_path.appendleft(k)
yield error
Expand Down Expand Up @@ -417,6 +419,8 @@ def descend(
schema=schema,
type_checker=evolved.TYPE_CHECKER,
)
if k == "$ref":
error.refs.appendleft(v)
if k not in {"if", "$ref"}:
error.schema_path.appendleft(k)
if path is not None:
Expand Down