Skip to content

Commit 9294f46

Browse files
committed
Improve internal terminology for retrieval URI
The retrieval URI was internally called the 'ref base' or 'schema URI', both of which suggest that it is definitively the base URI used for reference resolution. This makes it confusing that adjusting this URI might not change the base URI used. Instead, call it properly 'retrieval URI'. Base URI is newly introduced inside of the retrieve callback, after receiving both the `$id` (if any) and the retrieval URI.
1 parent b785746 commit 9294f46

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

src/check_jsonschema/schema_loader/main.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ def _get_schema_reader(self) -> LocalSchemaReader | HttpSchemaReader:
100100
f"detected parsed URL had an unrecognized scheme: {self.url_info}"
101101
)
102102

103-
def get_schema_ref_base(self) -> str | None:
104-
return self.reader.get_ref_base()
103+
def get_schema_retrieval_uri(self) -> str | None:
104+
return self.reader.get_retrieval_uri()
105105

106106
def get_schema(self) -> dict[str, t.Any]:
107107
return self.reader.read_schema()
@@ -113,7 +113,7 @@ def get_validator(
113113
format_opts: FormatOptions,
114114
fill_defaults: bool,
115115
) -> jsonschema.Validator:
116-
schema_uri = self.get_schema_ref_base()
116+
retrieval_uri = self.get_schema_retrieval_uri()
117117
schema = self.get_schema()
118118

119119
schema_dialect = schema.get("$schema")
@@ -123,7 +123,7 @@ def get_validator(
123123

124124
# reference resolution
125125
# with support for YAML, TOML, and other formats from the parsers
126-
reference_registry = make_reference_registry(self._parsers, schema_uri, schema)
126+
reference_registry = make_reference_registry(self._parsers, retrieval_uri, schema)
127127

128128
# get the correct validator class and check the schema under its metaschema
129129
validator_cls = jsonschema.validators.validator_for(schema)
@@ -147,7 +147,7 @@ def __init__(self, schema_name: str) -> None:
147147
self.schema_name = schema_name
148148
self._parsers = ParserSet()
149149

150-
def get_schema_ref_base(self) -> str | None:
150+
def get_schema_retrieval_uri(self) -> str | None:
151151
return None
152152

153153
def get_schema(self) -> dict[str, t.Any]:

src/check_jsonschema/schema_loader/readers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def __init__(self, filename: str) -> None:
3030
self.filename = str(self.path)
3131
self.parsers = ParserSet()
3232

33-
def get_ref_base(self) -> str:
33+
def get_retrieval_uri(self) -> str:
3434
return self.path.as_uri()
3535

3636
def _read_impl(self) -> t.Any:
@@ -55,7 +55,7 @@ def __init__(
5555
validation_callback=json.loads,
5656
)
5757

58-
def get_ref_base(self) -> str:
58+
def get_retrieval_uri(self) -> str:
5959
return self.url
6060

6161
def _read_impl(self) -> t.Any:

src/check_jsonschema/schema_loader/resolver.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,46 @@
1212

1313

1414
def make_reference_registry(
15-
parsers: ParserSet, schema_uri: str | None, schema: dict
15+
parsers: ParserSet, retrieval_uri: str | None, schema: dict
1616
) -> referencing.Registry:
17+
id_attribute_: t.Any = schema.get("$id")
18+
if isinstance(id_attribute_, str):
19+
id_attribute: str | None = id_attribute_
20+
else:
21+
id_attribute = None
22+
1723
schema_resource = referencing.Resource.from_contents(
1824
schema, default_specification=DRAFT202012
1925
)
2026
# mypy does not recognize that Registry is an `attrs` class and has `retrieve` as an
2127
# argument to its implicit initializer
2228
registry: referencing.Registry = referencing.Registry( # type: ignore[call-arg]
23-
retrieve=create_retrieve_callable(parsers, schema_uri)
29+
retrieve=create_retrieve_callable(parsers, retrieval_uri, id_attribute)
2430
)
2531

26-
if schema_uri is not None:
27-
registry = registry.with_resource(uri=schema_uri, resource=schema_resource)
28-
29-
id_attribute = schema.get("$id")
32+
if retrieval_uri is not None:
33+
registry = registry.with_resource(uri=retrieval_uri, resource=schema_resource)
3034
if id_attribute is not None:
3135
registry = registry.with_resource(uri=id_attribute, resource=schema_resource)
3236

3337
return registry
3438

3539

3640
def create_retrieve_callable(
37-
parser_set: ParserSet, schema_uri: str | None
41+
parser_set: ParserSet, retrieval_uri: str | None, id_attribute: str | None
3842
) -> t.Callable[[str], referencing.Resource[Schema]]:
43+
base_uri = id_attribute
44+
if base_uri is None:
45+
base_uri = retrieval_uri
46+
3947
def get_local_file(uri: str) -> t.Any:
4048
path = filename2path(uri)
4149
return parser_set.parse_file(path, "json")
4250

4351
def retrieve_reference(uri: str) -> referencing.Resource[Schema]:
4452
scheme = urllib.parse.urlsplit(uri).scheme
45-
if scheme == "" and schema_uri is not None:
46-
full_uri = urllib.parse.urljoin(schema_uri, uri)
53+
if scheme == "" and base_uri is not None:
54+
full_uri = urllib.parse.urljoin(base_uri, uri)
4755
else:
4856
full_uri = uri
4957

0 commit comments

Comments
 (0)