-
-
Notifications
You must be signed in to change notification settings - Fork 593
Fix relative references #306
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
Changes from all commits
37d4f0a
d9efe69
aab7523
e74dedc
62640dd
e455439
79baa6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,9 @@ | |
from jsonschema.tests.compat import mock, unittest | ||
from jsonschema.validators import ( | ||
RefResolutionError, UnknownType, Draft3Validator, | ||
Draft4Validator, RefResolver, create, extend, validator_for, validate, | ||
Draft4Validator, RefResolver, create, extend, validator_for, validate | ||
) | ||
from jsonschema.compat import unquote | ||
|
||
|
||
class TestCreateAndExtend(unittest.TestCase): | ||
|
@@ -771,6 +772,20 @@ class TestRefResolver(unittest.TestCase): | |
stored_uri = "foo://stored" | ||
stored_schema = {"stored" : "schema"} | ||
|
||
def prev_resolve_fragment(self, document, fragment): | ||
fragment = fragment.lstrip(u"/") | ||
parts = unquote(fragment).split(u"/") if fragment else [] | ||
|
||
for part in parts: | ||
try: | ||
document = document[part] | ||
except (TypeError, LookupError): | ||
raise RefResolutionError( | ||
"Unresolvable JSON pointer: %r" % fragment | ||
) | ||
|
||
return document | ||
|
||
def setUp(self): | ||
self.referrer = {} | ||
self.store = {self.stored_uri : self.stored_schema} | ||
|
@@ -898,6 +913,63 @@ def test_helpful_error_message_on_failed_pop_scope(self): | |
resolver.pop_scope() | ||
self.assertIn("Failed to pop the scope", str(exc.exception)) | ||
|
||
def test_prev_resolve_fragment_raises_exception(self): | ||
schema = { | ||
"definitions": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This schema seems like it should be easy to simplify. |
||
"Pet": { | ||
"properties": { | ||
"category": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/Category" | ||
} | ||
} | ||
} | ||
}, | ||
"Category": { | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
} | ||
} | ||
} | ||
} | ||
resolver = RefResolver.from_schema(schema) | ||
resolver.resolve_fragment = mock.MagicMock( | ||
side_effect=self.prev_resolve_fragment) | ||
self.assertRaises(RefResolutionError, | ||
resolver.resolve_fragment, | ||
schema, | ||
"#/definitions/Category") | ||
|
||
def test_resolve_fragment(self): | ||
schema = { | ||
"definitions": { | ||
"Pet": { | ||
"properties": { | ||
"category": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/Category" | ||
} | ||
} | ||
} | ||
}, | ||
"Category": { | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
} | ||
} | ||
} | ||
} | ||
resolver = RefResolver.from_schema(schema) | ||
self.assertEqual(resolver.resolve_fragment | ||
(schema, "#/definitions/Category"), | ||
schema['definitions']['Category']) | ||
|
||
|
||
class UniqueTupleItemsMixin(object): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,9 +14,8 @@ | |
Sequence, urljoin, urlsplit, urldefrag, unquote, urlopen, | ||
str_types, int_types, iteritems, lru_cache, | ||
) | ||
from jsonschema.exceptions import ErrorTree # Backwards compatibility # noqa | ||
from jsonschema.exceptions import RefResolutionError, SchemaError, UnknownType | ||
|
||
from jsonschema.exceptions import ErrorTree # Backwards compatibility # noqa | ||
|
||
_unset = _utils.Unset() | ||
|
||
|
@@ -356,7 +355,7 @@ def resolve_fragment(self, document, fragment): | |
|
||
""" | ||
|
||
fragment = fragment.lstrip(u"/") | ||
fragment = fragment.lstrip(u"#").lstrip(u"/") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems likely to be misplaced overall to me (which maybe is another reason why an integration test would be good). This method is about resolving fragments -- fragments don't start with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it, my mistake There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you want functionality, perhaps in another method, to convert a pointer into a fragment, by stripping the |
||
parts = unquote(fragment).split(u"/") if fragment else [] | ||
|
||
for part in parts: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like quite a lot of not-testing-specific-looking code making an appearance in a test. I don't have a specific recommendation yet on how to remove it, but it doesn't look right to me, especially not in combination with being patched into place (I'd like to sometime soon remove the
mock
dependency entirely, but alas).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok sorry about that, I can remove some of the unnecessary parts of that method. I only put them in there to replicate the original method as closely as possible.