Skip to content

Commit c1d3e8b

Browse files
committed
jsonschema 4.18 compatibility
1 parent efb7b09 commit c1d3e8b

File tree

4 files changed

+118
-88
lines changed

4 files changed

+118
-88
lines changed

openapi_spec_validator/validation/validators.py

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from jsonschema.exceptions import ValidationError
1515
from jsonschema.protocols import Validator
1616
from jsonschema.validators import RefResolver
17-
from jsonschema_spec.accessors import SpecAccessor
18-
from jsonschema_spec.paths import Spec
17+
from jsonschema_spec.accessors import ResolverAccessor
18+
from jsonschema_spec.paths import SchemaPath
1919

2020
from openapi_spec_validator.validation.decorators import ValidationErrorWrapper
2121
from openapi_spec_validator.validation.exceptions import (
@@ -84,12 +84,10 @@ def iter_errors(
8484
) -> Iterator[ValidationError]:
8585
self.operation_ids_registry = []
8686
self.schema_ids_registry = []
87-
self.resolver = self._get_resolver(spec_url, instance)
8887

8988
yield from self.schema_validator.iter_errors(instance)
9089

91-
accessor = SpecAccessor(instance, self.resolver)
92-
spec = Spec(accessor)
90+
spec = SchemaPath.from_dict(instance, base_uri=spec_url)
9391
if "paths" in spec:
9492
paths = spec / "paths"
9593
yield from self._iter_paths_errors(paths)
@@ -98,17 +96,12 @@ def iter_errors(
9896
components = spec / "components"
9997
yield from self._iter_components_errors(components)
10098

101-
def _get_resolver(
102-
self, base_uri: str, referrer: Mapping[Hashable, Any]
103-
) -> RefResolver:
104-
return RefResolver(base_uri, referrer, handlers=self.resolver_handlers)
105-
106-
def _iter_paths_errors(self, paths: Spec) -> Iterator[ValidationError]:
99+
def _iter_paths_errors(self, paths: SchemaPath) -> Iterator[ValidationError]:
107100
for url, path_item in paths.items():
108101
yield from self._iter_path_errors(url, path_item)
109102

110103
def _iter_path_errors(
111-
self, url: str, path_item: Spec
104+
self, url: str, path_item: SchemaPath
112105
) -> Iterator[ValidationError]:
113106
parameters = None
114107
if "parameters" in path_item:
@@ -127,8 +120,8 @@ def _iter_operation_errors(
127120
self,
128121
url: str,
129122
name: str,
130-
operation: Spec,
131-
path_parameters: Optional[Spec],
123+
operation: SchemaPath,
124+
path_parameters: Optional[SchemaPath],
132125
) -> Iterator[ValidationError]:
133126
assert self.operation_ids_registry is not None
134127

@@ -168,13 +161,13 @@ def _iter_operation_errors(
168161
return
169162

170163
def _iter_responses_errors(
171-
self, responses: Spec
164+
self, responses: SchemaPath
172165
) -> Iterator[ValidationError]:
173166
for response_code, response in responses.items():
174167
yield from self._iter_response_errors(response_code, response)
175168

176169
def _iter_response_errors(
177-
self, response_code: str, response: Spec
170+
self, response_code: str, response: SchemaPath
178171
) -> Iterator[ValidationError]:
179172
# openapi 2
180173
if "schema" in response:
@@ -185,18 +178,18 @@ def _iter_response_errors(
185178
content = response / "content"
186179
yield from self._iter_content_errors(content)
187180

188-
def _iter_content_errors(self, content: Spec) -> Iterator[ValidationError]:
181+
def _iter_content_errors(self, content: SchemaPath) -> Iterator[ValidationError]:
189182
for mimetype, media_type in content.items():
190183
yield from self._iter_media_type_errors(mimetype, media_type)
191184

192185
def _iter_media_type_errors(
193-
self, mimetype: str, media_type: Spec
186+
self, mimetype: str, media_type: SchemaPath
194187
) -> Iterator[ValidationError]:
195188
if "schema" in media_type:
196189
schema = media_type / "schema"
197190
yield from self._iter_schema_errors(schema)
198191

199-
def _get_path_param_names(self, params: Spec) -> Iterator[str]:
192+
def _get_path_param_names(self, params: SchemaPath) -> Iterator[str]:
200193
for param in params:
201194
if param["in"] == "path":
202195
yield param["name"]
@@ -207,7 +200,7 @@ def _get_path_params_from_url(self, url: str) -> Iterator[str]:
207200
return filter(None, path_params)
208201

209202
def _iter_parameters_errors(
210-
self, parameters: Spec
203+
self, parameters: SchemaPath
211204
) -> Iterator[ValidationError]:
212205
seen = set()
213206
for parameter in parameters:
@@ -221,7 +214,7 @@ def _iter_parameters_errors(
221214
seen.add(key)
222215

223216
def _iter_parameter_errors(
224-
self, parameter: Spec
217+
self, parameter: SchemaPath
225218
) -> Iterator[ValidationError]:
226219
if "schema" in parameter:
227220
schema = parameter / "schema"
@@ -234,7 +227,7 @@ def _iter_parameter_errors(
234227
yield from self._iter_value_errors(parameter, default)
235228

236229
def _iter_value_errors(
237-
self, schema: Spec, value: Any
230+
self, schema: SchemaPath, value: Any
238231
) -> Iterator[ValidationError]:
239232
with schema.open() as content:
240233
validator = self.value_validator_class(
@@ -245,7 +238,7 @@ def _iter_value_errors(
245238
yield from validator.iter_errors(value)
246239

247240
def _iter_schema_errors(
248-
self, schema: Spec, require_properties: bool = True
241+
self, schema: SchemaPath, require_properties: bool = True
249242
) -> Iterator[ValidationError]:
250243
if not hasattr(schema.content(), "__getitem__"):
251244
return
@@ -329,11 +322,11 @@ def _iter_schema_errors(
329322
yield from self._iter_value_errors(schema, default)
330323

331324
def _iter_components_errors(
332-
self, components: Spec
325+
self, components: SchemaPath
333326
) -> Iterator[ValidationError]:
334327
schemas = components.get("schemas", {})
335328
yield from self._iter_schemas_errors(schemas)
336329

337-
def _iter_schemas_errors(self, schemas: Spec) -> Iterator[ValidationError]:
330+
def _iter_schemas_errors(self, schemas: SchemaPath) -> Iterator[ValidationError]:
338331
for _, schema in schemas.items():
339332
yield from self._iter_schema_errors(schema)

0 commit comments

Comments
 (0)