Skip to content

Commit 13251e5

Browse files
committed
Fix nested local reference resolution
1 parent e210bb2 commit 13251e5

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

src/check_jsonschema/schema_loader/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ def _get_validator(
154154
) -> jsonschema.protocols.Validator:
155155
retrieval_uri = self.get_schema_retrieval_uri()
156156
schema = self.get_schema()
157+
if retrieval_uri is not None and "$id" not in schema:
158+
schema = {"$id": retrieval_uri, **schema}
157159
schema_dialect = _dialect_of_schema(schema)
158160

159161
# format checker (which may be None)

tests/acceptance/test_local_relative_ref.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@
2929
CASE2_PASSING_DOCUMENT = {"test": "some data"}
3030
CASE2_FAILING_DOCUMENT = {"test": {"foo": "bar"}}
3131

32+
CASE3_MAIN_SCHEMA = {
33+
"type": "object",
34+
"properties": {
35+
"pupils": {
36+
"type": "array",
37+
"items": {"$ref": "../person/person.schema.json"},
38+
}
39+
},
40+
}
41+
CASE3_PERSON_SCHEMA = {
42+
"type": "object",
43+
"properties": {"address": {"$ref": "../address/address.schema.json"}},
44+
}
45+
CASE3_ADDRESS_SCHEMA = {
46+
"type": "object",
47+
"properties": {"zip_code": {"type": "number"}},
48+
}
49+
CASE3_PASSING_DOCUMENT = {"pupils": [{"address": {"zip_code": 12345}}]}
50+
3251

3352
def _prep_files(tmp_path, main_schema, other_schema_data, instance):
3453
main_schemafile = tmp_path / "main_schema.json"
@@ -75,6 +94,24 @@ def test_local_ref_schema(
7594
run_line_simple(["--schemafile", schemafile, str(doc)])
7695

7796

97+
def test_nested_local_ref_schema(run_line_simple, tmp_path):
98+
school_dir = tmp_path / "school"
99+
person_dir = tmp_path / "person"
100+
address_dir = tmp_path / "address"
101+
school_dir.mkdir()
102+
person_dir.mkdir()
103+
address_dir.mkdir()
104+
105+
main_schemafile = school_dir / "school.schema.json"
106+
main_schemafile.write_text(json.dumps(CASE3_MAIN_SCHEMA))
107+
(person_dir / "person.schema.json").write_text(json.dumps(CASE3_PERSON_SCHEMA))
108+
(address_dir / "address.schema.json").write_text(json.dumps(CASE3_ADDRESS_SCHEMA))
109+
doc = school_dir / "school.example.json"
110+
doc.write_text(json.dumps(CASE3_PASSING_DOCUMENT))
111+
112+
run_line_simple(["--schemafile", str(main_schemafile), str(doc)])
113+
114+
78115
@pytest.mark.parametrize(
79116
"main_schema, other_schema_data, instance, expect_err",
80117
[

0 commit comments

Comments
 (0)