From eeff41adc80ca2616160ab1c41c9ca2dd9885650 Mon Sep 17 00:00:00 2001 From: p1c2u Date: Fri, 13 Oct 2023 10:24:11 +0000 Subject: [PATCH] Validator accept any schema --- openapi_spec_validator/schemas/types.py | 6 +++++ .../validation/validators.py | 26 +++++++++---------- poetry.lock | 8 +++--- pyproject.toml | 2 +- .../integration/validation/test_validators.py | 7 ++++- 5 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 openapi_spec_validator/schemas/types.py diff --git a/openapi_spec_validator/schemas/types.py b/openapi_spec_validator/schemas/types.py new file mode 100644 index 0000000..d48c516 --- /dev/null +++ b/openapi_spec_validator/schemas/types.py @@ -0,0 +1,6 @@ +from typing import Union + +from jsonschema_path.paths import SchemaPath +from jsonschema_path.typing import Schema + +AnySchema = Union[Schema, SchemaPath] diff --git a/openapi_spec_validator/validation/validators.py b/openapi_spec_validator/validation/validators.py index dbfd8ea..cb8f5ad 100644 --- a/openapi_spec_validator/validation/validators.py +++ b/openapi_spec_validator/validation/validators.py @@ -18,6 +18,7 @@ from openapi_spec_validator.schemas import openapi_v2_schema_validator from openapi_spec_validator.schemas import openapi_v30_schema_validator from openapi_spec_validator.schemas import openapi_v31_schema_validator +from openapi_spec_validator.schemas.types import AnySchema from openapi_spec_validator.validation import keywords from openapi_spec_validator.validation.decorators import unwraps_iter from openapi_spec_validator.validation.decorators import wraps_cached_iter @@ -39,11 +40,10 @@ class SpecValidator: def __init__( self, - schema: Schema, + schema: AnySchema, base_uri: str = "", spec_url: Optional[str] = None, ) -> None: - self.schema = schema if spec_url is not None: warnings.warn( "spec_url parameter is deprecated. " "Use base_uri instead.", @@ -52,11 +52,16 @@ def __init__( base_uri = spec_url self.base_uri = base_uri - self.spec = SchemaPath.from_dict( - self.schema, - base_uri=self.base_uri, - handlers=self.resolver_handlers, - ) + if isinstance(schema, SchemaPath): + self.schema_path = schema + self.schema = schema.contents() + else: + self.schema = schema + self.schema_path = SchemaPath.from_dict( + self.schema, + base_uri=self.base_uri, + handlers=self.resolver_handlers, + ) self.keyword_validators_registry = KeywordValidatorRegistry( self.keyword_validators @@ -84,12 +89,7 @@ def root_validator(self) -> keywords.RootValidator: def iter_errors(self) -> Iterator[ValidationError]: yield from self.schema_validator.iter_errors(self.schema) - spec = SchemaPath.from_dict( - self.schema, - base_uri=self.base_uri, - handlers=self.resolver_handlers, - ) - yield from self.root_validator(spec) + yield from self.root_validator(self.schema_path) class OpenAPIV2SpecValidator(SpecValidator): diff --git a/poetry.lock b/poetry.lock index 8daf93d..ea97d5c 100644 --- a/poetry.lock +++ b/poetry.lock @@ -613,13 +613,13 @@ format-nongpl = ["fqdn", "idna", "isoduration", "jsonpointer (>1.13)", "rfc3339- [[package]] name = "jsonschema-path" -version = "0.3.0" +version = "0.3.1" description = "JSONSchema Spec with object-oriented paths" optional = false python-versions = ">=3.8.0,<4.0.0" files = [ - {file = "jsonschema_path-0.3.0-py3-none-any.whl", hash = "sha256:f165924e5f61e8d6633fdd5a620620b1c1271e81c7592286357e54e2ceeecf77"}, - {file = "jsonschema_path-0.3.0.tar.gz", hash = "sha256:4b2791105ec16e47e5d89c0d08220d3049b9e6dd735fc2e07a0132f5d6b769c7"}, + {file = "jsonschema_path-0.3.1-py3-none-any.whl", hash = "sha256:06f01b1848a28963f49a17730e11204d252aa6ff5db4ef84ec77e5ac93cfa831"}, + {file = "jsonschema_path-0.3.1.tar.gz", hash = "sha256:07ea584b5c9b41a614b4d011c5575955676f48d0abbfd93d9ea8e933018d716d"}, ] [package.dependencies] @@ -1728,4 +1728,4 @@ docs = [] [metadata] lock-version = "2.0" python-versions = "^3.8.0" -content-hash = "0933a35821428ee7eb09dd3d77c2762a283c6e5fec5786d4bedbdfddd26c621c" +content-hash = "a438d6b1d1eba8ef9369a1a8808825ae640f0920b93aafec2406b710bf01bd03" diff --git a/pyproject.toml b/pyproject.toml index 4163fe7..741dc01 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -56,7 +56,7 @@ jsonschema = "^4.18.0" openapi-schema-validator = "^0.6.0" python = "^3.8.0" importlib-resources = {version = ">=5.8,<7.0", python = "<3.9" } -jsonschema-path = "^0.3.0" +jsonschema-path = "^0.3.1" lazy-object-proxy = "^1.7.1" [tool.poetry.extras] diff --git a/tests/integration/validation/test_validators.py b/tests/integration/validation/test_validators.py index 0ff61c5..0cb04b4 100644 --- a/tests/integration/validation/test_validators.py +++ b/tests/integration/validation/test_validators.py @@ -1,6 +1,7 @@ import pytest from referencing.exceptions import Unresolvable +from jsonschema_path import SchemaPath from openapi_spec_validator import OpenAPIV2SpecValidator from openapi_spec_validator import OpenAPIV30SpecValidator from openapi_spec_validator import OpenAPIV31SpecValidator @@ -23,7 +24,11 @@ def test_valid(self, factory, spec_file): spec_path = self.local_test_suite_file_path(spec_file) spec = factory.spec_from_file(spec_path) spec_url = factory.spec_file_url(spec_path) - validator = OpenAPIV2SpecValidator(spec, base_uri=spec_url) + schema_path = SchemaPath.from_dict( + spec, + base_uri=spec_url, + ) + validator = OpenAPIV2SpecValidator(schema_path) validator.validate()