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

schemas and validators lazy loading proxy #170

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions openapi_spec_validator/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
"""OpenAIP spec validator schemas module."""
from openapi_spec_validator.schemas.utils import get_schema
from functools import partial

from lazy_object_proxy import Proxy

from openapi_spec_validator.schemas.utils import get_schema_content

__all__ = ["schema_v2", "schema_v3", "schema_v30", "schema_v31"]

schema_v2, _ = get_schema("2.0")
schema_v30, _ = get_schema("3.0")
schema_v31, _ = get_schema("3.1")
get_schema_content_v2 = partial(get_schema_content, "2.0")
get_schema_content_v30 = partial(get_schema_content, "3.0")
get_schema_content_v31 = partial(get_schema_content, "3.1")

schema_v2 = Proxy(get_schema_content_v2)
schema_v30 = Proxy(get_schema_content_v30)
schema_v31 = Proxy(get_schema_content_v31)

# alias to the latest v3 version
schema_v3 = schema_v31
5 changes: 5 additions & 0 deletions openapi_spec_validator/schemas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ def get_schema(version: str) -> Tuple[Mapping[Hashable, Any], str]:
with importlib_resources.as_file(ref) as resource_path:
schema_path_full = path.join(path.dirname(__file__), resource_path)
return FilePathReader(schema_path_full).read()


def get_schema_content(version: str) -> Mapping[Hashable, Any]:
content, _ = get_schema(version)
return content
24 changes: 18 additions & 6 deletions openapi_spec_validator/validation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from functools import partial

from jsonschema.validators import Draft4Validator
from jsonschema.validators import Draft202012Validator
from jsonschema_spec.handlers import default_handlers
from lazy_object_proxy import Proxy
from openapi_schema_validator import oas30_format_checker
from openapi_schema_validator import oas31_format_checker
from openapi_schema_validator.validators import OAS30Validator
Expand All @@ -19,31 +22,40 @@
]

# v2.0 spec
openapi_v2_schema_validator = Draft4Validator(schema_v2)
openapi_v2_spec_validator = SpecValidator(
get_openapi_v2_schema_validator = partial(Draft4Validator, schema_v2)
openapi_v2_schema_validator = Proxy(get_openapi_v2_schema_validator)
get_openapi_v2_spec_validator = partial(
SpecValidator,
openapi_v2_schema_validator,
OAS30Validator,
oas30_format_checker,
resolver_handlers=default_handlers,
)
openapi_v2_spec_validator = Proxy(get_openapi_v2_spec_validator)

# v3.0 spec
openapi_v30_schema_validator = Draft4Validator(schema_v30)
openapi_v30_spec_validator = SpecValidator(
get_openapi_v30_schema_validator = partial(Draft4Validator, schema_v30)
openapi_v30_schema_validator = Proxy(get_openapi_v30_schema_validator)
get_openapi_v30_spec_validator = partial(
SpecValidator,
openapi_v30_schema_validator,
OAS30Validator,
oas30_format_checker,
resolver_handlers=default_handlers,
)
openapi_v30_spec_validator = Proxy(get_openapi_v30_spec_validator)

# v3.1 spec
openapi_v31_schema_validator = Draft202012Validator(schema_v31)
openapi_v31_spec_validator = SpecValidator(
get_openapi_v31_schema_validator = partial(Draft202012Validator, schema_v31)
openapi_v31_schema_validator = Proxy(get_openapi_v31_schema_validator)
get_openapi_v31_spec_validator = partial(
SpecValidator,
openapi_v31_schema_validator,
OAS31Validator,
oas31_format_checker,
resolver_handlers=default_handlers,
)
openapi_v31_spec_validator = Proxy(get_openapi_v31_spec_validator)

# alias to the latest v3 version
openapi_v3_spec_validator = openapi_v31_spec_validator
11 changes: 10 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ strict = true
module = "jsonschema.*"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "lazy_object_proxy.*"
ignore_missing_imports = true

[tool.poetry]
name = "openapi-spec-validator"
version = "0.5.0a3"
Expand Down Expand Up @@ -47,6 +51,7 @@ PyYAML = ">=5.1"
requests = {version = "*", optional = true}
importlib-resources = "^5.8.0"
jsonschema-spec = "^0.1.1"
lazy-object-proxy = "^1.7.1"

[tool.poetry.extras]
dev = ["pre-commit"]
Expand Down