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 extra sibling attributes #35

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
10 changes: 9 additions & 1 deletion jsonref.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import functools
import json
import warnings
import copy

try:
from collections.abc import Mapping, MutableMapping, Sequence
Expand Down Expand Up @@ -134,6 +135,10 @@ def __init__(
self.store = _store # Use the same object to be shared with children
if self.store is None:
self.store = _URIDict()
self.extra = None
if isinstance(refobj, Mapping) and len(refobj.keys()) > 1:
self.extra = copy.deepcopy(refobj)
del self.extra["$ref"]

@property
def _ref_kwargs(self):
Expand Down Expand Up @@ -192,7 +197,10 @@ def resolve_pointer(self, document, pointer):
except ValueError:
pass
try:
document = document[part]
if self.extra:
document = {**document[part], **self.extra}
else:
document = document[part]
except (TypeError, LookupError) as e:
self._error("Unresolvable JSON pointer: %r" % pointer, cause=e)
return document
Expand Down
48 changes: 48 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,54 @@ def test_actual_references_not_copies(self):
assert result["c"].__subject__ is result["a"]
assert result["d"].__subject__ is result["a"]

def test_ignored_sibling_attributes(self):
json = {
"a": {"type": "object", "properties": {"foo": {"type": "string"}}},
"b": {"extra": "foobar", "$ref": "#/a"},
"c": {"extra": { "more": "bar", "$ref": "#/a"} },
"d": {"deep": { "$ref": "#/a", "more": { "$ref": "#/b" } } },
}
result = JsonRef.replace_refs(json)
# because we deepcopy, use '==' instead of 'is"
assert result["b"] == {
"extra": "foobar",
"type": "object",
"properties": {"foo": {"type": "string"}},
}
assert result["c"] == {
"extra": {
"more": "bar",
"type": "object",
"properties": {"foo": {"type": "string"}}
}
}
assert result["d"] == {
"deep": {
"more": {
"extra": "foobar",
"type": "object",
"properties": {"foo": {"type": "string"}},
},
"type": "object",
"properties": {"foo": {"type": "string"}}
}
}

def test_extra_sibling_attributes_list_ref(self):
json = {
"a": ["target"],
"b": {"extra": "foobar", "$ref": "#/a"},
}
result = JsonRef.replace_refs(json)
with pytest.raises(JsonRefError) as excinfo:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bad behavior, even if the extra attributes are opt-in. This clearly violates the spec stipulation that extra attributes should be ignored. I think when turning on a merge-attributes mode, it would only do anything if the ref points to a dictionary, and just ignore them otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough

result["b"].__subject__
e = excinfo.value
assert e.reference == json["b"]
assert e.uri == "#/a"
assert e.base_uri == ""
assert e.path == ["b"]
assert type(e.cause) == TypeError

def test_recursive_data_structures_local(self):
json = {"a": "foobar", "b": {"$ref": "#"}}
result = JsonRef.replace_refs(json)
Expand Down