Skip to content

RefResolver export function for resolving references #419

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

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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ _static
_templates

TODO
.idea
.python-version
.eggs
jsonschema.egg-info
6 changes: 6 additions & 0 deletions jsonschema/tests/fixtures/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pathlib import Path
from json import loads

apple = loads(open(str(Path(__file__).parents[0]) + '/apple.json').read())
orange = loads(open(str(Path(__file__).parents[0]) + '/orange.json').read())
tree = loads(open(str(Path(__file__).parents[0]) + '/tree.json').read())
19 changes: 19 additions & 0 deletions jsonschema/tests/fixtures/apple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$id": "apples",
"type": "object",
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {
"name": {
"type": "string"
},
"quantity": {
"type": "integer"
},
"types": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
19 changes: 19 additions & 0 deletions jsonschema/tests/fixtures/orange.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$id": "oranges",
"type": "object",
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {
"name": {
"type": "string"
},
"quantity": {
"type": "integer"
},
"types": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
21 changes: 21 additions & 0 deletions jsonschema/tests/fixtures/tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$id": "tree",
"type": "object",
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {
"name": {
"type": "string"
},
"fruits": {
"type": "object",
"properties": {
"apples": {
"$ref": "apples"
},
"oranges": {
"$ref": "oranges"
}
}
}
}
}
20 changes: 20 additions & 0 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
from twisted.trial.unittest import SynchronousTestCase
import attr

from jsonschema import (
SchemaError,
ValidationError,
_types,
)
from jsonschema.tests.compat import mock
from jsonschema.tests.fixtures import apple, orange, tree
from jsonschema import FormatChecker, TypeChecker, exceptions, validators
from jsonschema.compat import PY3, pathname2url
import jsonschema
Expand Down Expand Up @@ -1755,6 +1762,19 @@ 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_export_resolved_references(self):
ref_resolver = validators.RefResolver(
'',
None,
store={
'apples': apple,
'oranges': orange,
})
resolved_tree_refs = ref_resolver.export_resolved_references(tree)
self.assertTrue(apple, resolved_tree_refs['properties']['fruits']['properties']['apples'])
self.assertTrue(orange, resolved_tree_refs['properties']['fruits']['properties']['oranges'])



def sorted_errors(errors):
def key(error):
Expand Down
28 changes: 28 additions & 0 deletions jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numbers

from six import add_metaclass
from urllib.parse import urlparse

from jsonschema import (
_legacy_validators,
Expand Down Expand Up @@ -832,6 +833,33 @@ def resolve_remote(self, uri):
self.store[uri] = result
return result

def export_resolved_references(self, schema: dict):
"""
Resolves json references and merges them into a consolidated schema for validation purposes
:param schema:
:return: schema merged with resolved references
"""
if len(self.store) <= 2:
return RefResolutionError('RefResolver does not have any additional ' +\
'referenced schemas outside of draft 3 & 4')

if isinstance(schema, dict):
for key, value in schema.items():
if key == "$ref":
ref_schema = self.resolve(urlparse(value).path)
if ref_schema:
return ref_schema[1]

resolved_ref = self.export_resolved_references(value)
if resolved_ref:
schema[key] = resolved_ref
elif isinstance(schema, list):
for (idx, value) in enumerate(schema):
resolved_ref = self.export_resolved_references(value)
if resolved_ref:
schema[idx] = resolved_ref
return schema


def validate(instance, schema, cls=None, *args, **kwargs):
"""
Expand Down