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

Resolve references #1006

Closed
wants to merge 4 commits into from
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
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 json import loads
from pathlib import Path

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"
}
}
}
}
}
14 changes: 14 additions & 0 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
validators,
)
from jsonschema.tests._helpers import bug
from jsonschema.tests.fixtures import apple, orange, tree


def fail(validator, errors, instance, schema):
Expand Down Expand Up @@ -2223,6 +2224,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
29 changes: 28 additions & 1 deletion jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from collections.abc import Sequence
from functools import lru_cache
from operator import methodcaller
from urllib.parse import unquote, urldefrag, urljoin, urlsplit
from urllib.parse import unquote, urldefrag, urljoin, urlparse, urlsplit
from urllib.request import urlopen
from warnings import warn
import contextlib
Expand Down Expand Up @@ -1011,6 +1011,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


_SUBSCHEMAS_KEYWORDS = ("$id", "id", "$anchor", "$dynamicAnchor")

Expand Down