Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Validators refactor #286

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

Merged
merged 1 commit into from
Oct 9, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions docs/python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ You can also validate spec from url:

In order to explicitly validate a:

* Swagger / OpenAPI 2.0 spec, import ``openapi_v2_spec_validator``
* OpenAPI 3.0 spec, import ``openapi_v30_spec_validator``
* OpenAPI 3.1 spec, import ``openapi_v31_spec_validator``
* Swagger / OpenAPI 2.0 spec, import ``OpenAPIV2SpecValidator``
* OpenAPI 3.0 spec, import ``OpenAPIV30SpecValidator``
* OpenAPI 3.1 spec, import ``OpenAPIV31SpecValidator``

and pass the validator to ``validate_spec`` or ``validate_spec_url`` function:
and pass the validator class to ``validate_spec`` or ``validate_spec_url`` function:

.. code:: python

validate_spec(spec_dict, validator=openapi_v31_spec_validator)
validate_spec(spec_dict, cls=OpenAPIV31SpecValidator)

You can also explicitly import ``openapi_v3_spec_validator`` which is a shortcut to the latest v3 release.
You can also explicitly import ``OpenAPIV3SpecValidator`` which is a shortcut to the latest v3 release.

If you want to iterate through validation errors:

.. code:: python

from openapi_spec_validator import openapi_v3_spec_validator
from openapi_spec_validator import OpenAPIV31SpecValidator

errors_iterator = openapi_v3_spec_validator.iter_errors(spec)
errors_iterator = OpenAPIV31SpecValidator(spec).iter_errors()
8 changes: 8 additions & 0 deletions openapi_spec_validator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""OpenAPI spec validator module."""
from openapi_spec_validator.shortcuts import validate_spec
from openapi_spec_validator.shortcuts import validate_spec_url
from openapi_spec_validator.validation import OpenAPIV2SpecValidator
from openapi_spec_validator.validation import OpenAPIV3SpecValidator
from openapi_spec_validator.validation import OpenAPIV30SpecValidator
from openapi_spec_validator.validation import OpenAPIV31SpecValidator
from openapi_spec_validator.validation import openapi_v2_spec_validator
from openapi_spec_validator.validation import openapi_v3_spec_validator
from openapi_spec_validator.validation import openapi_v30_spec_validator
Expand All @@ -17,6 +21,10 @@
"openapi_v3_spec_validator",
"openapi_v30_spec_validator",
"openapi_v31_spec_validator",
"OpenAPIV2SpecValidator",
"OpenAPIV3SpecValidator",
"OpenAPIV30SpecValidator",
"OpenAPIV31SpecValidator",
"validate_spec",
"validate_spec_url",
]
27 changes: 15 additions & 12 deletions openapi_spec_validator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

from openapi_spec_validator.readers import read_from_filename
from openapi_spec_validator.readers import read_from_stdin
from openapi_spec_validator.validation import openapi_spec_validator_proxy
from openapi_spec_validator.validation import openapi_v2_spec_validator
from openapi_spec_validator.validation import openapi_v30_spec_validator
from openapi_spec_validator.validation import openapi_v31_spec_validator
from openapi_spec_validator.shortcuts import get_validator_cls
from openapi_spec_validator.validation import OpenAPIV2SpecValidator
from openapi_spec_validator.validation import OpenAPIV30SpecValidator
from openapi_spec_validator.validation import OpenAPIV31SpecValidator

logger = logging.getLogger(__name__)
logging.basicConfig(
Expand Down Expand Up @@ -91,19 +91,22 @@ def main(args: Optional[Sequence[str]] = None) -> None:

# choose the validator
validators = {
"detect": openapi_spec_validator_proxy,
"2.0": openapi_v2_spec_validator,
"3.0": openapi_v30_spec_validator,
"3.1": openapi_v31_spec_validator,
"2.0": OpenAPIV2SpecValidator,
"3.0": OpenAPIV30SpecValidator,
"3.1": OpenAPIV31SpecValidator,
# backward compatibility
"3.0.0": openapi_v30_spec_validator,
"3.1.0": openapi_v31_spec_validator,
"3.0.0": OpenAPIV30SpecValidator,
"3.1.0": OpenAPIV31SpecValidator,
}
validator = validators[args_parsed.schema]
if args_parsed.schema == "detect":
validator_cls = get_validator_cls(spec)
else:
validator_cls = validators[args_parsed.schema]

validator = validator_cls(spec, base_uri=base_uri)
# validate
try:
validator.validate(spec, base_uri=base_uri)
validator.validate()
except ValidationError as exc:
print_validationerror(filename, exc, args_parsed.errors)
sys.exit(1)
Expand Down
6 changes: 5 additions & 1 deletion openapi_spec_validator/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
class OpenAPISpecValidatorError(Exception):
class OpenAPIError(Exception):
pass


class OpenAPISpecValidatorError(OpenAPIError):
pass
10 changes: 10 additions & 0 deletions openapi_spec_validator/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""OpenAIP spec validator schemas module."""
from functools import partial

from jsonschema.validators import Draft4Validator
from jsonschema.validators import Draft202012Validator
from lazy_object_proxy import Proxy

from openapi_spec_validator.schemas.utils import get_schema_content
Expand All @@ -17,3 +19,11 @@

# alias to the latest v3 version
schema_v3 = schema_v31

get_openapi_v2_schema_validator = partial(Draft4Validator, schema_v2)
get_openapi_v30_schema_validator = partial(Draft4Validator, schema_v30)
get_openapi_v31_schema_validator = partial(Draft202012Validator, schema_v31)

openapi_v2_schema_validator = Proxy(get_openapi_v2_schema_validator)
openapi_v30_schema_validator = Proxy(get_openapi_v30_schema_validator)
openapi_v31_schema_validator = Proxy(get_openapi_v31_schema_validator)
44 changes: 36 additions & 8 deletions openapi_spec_validator/shortcuts.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,55 @@
"""OpenAPI spec validator shortcuts module."""
from typing import Any
from typing import Hashable
import warnings
from typing import Mapping
from typing import Optional
from typing import Type

from jsonschema_spec.handlers import all_urls_handler
from jsonschema_spec.typing import Schema

from openapi_spec_validator.validation import openapi_spec_validator_proxy
from openapi_spec_validator.validation import OpenAPIV2SpecValidator
from openapi_spec_validator.validation import OpenAPIV30SpecValidator
from openapi_spec_validator.validation import OpenAPIV31SpecValidator
from openapi_spec_validator.validation.finders import SpecFinder
from openapi_spec_validator.validation.finders import SpecVersion
from openapi_spec_validator.validation.protocols import SupportsValidation
from openapi_spec_validator.validation.types import SpecValidatorType
from openapi_spec_validator.validation.validators import SpecValidator

SPECS: Mapping[SpecVersion, SpecValidatorType] = {
SpecVersion("swagger", "2.0"): OpenAPIV2SpecValidator,
SpecVersion("openapi", "3.0"): OpenAPIV30SpecValidator,
SpecVersion("openapi", "3.1"): OpenAPIV31SpecValidator,
}


def get_validator_cls(spec: Schema) -> SpecValidatorType:
return SpecFinder(SPECS).find(spec)


def validate_spec(
spec: Mapping[Hashable, Any],
spec: Schema,
base_uri: str = "",
validator: SupportsValidation = openapi_spec_validator_proxy,
validator: Optional[SupportsValidation] = None,
cls: Optional[SpecValidatorType] = None,
spec_url: Optional[str] = None,
) -> None:
return validator.validate(spec, base_uri=base_uri, spec_url=spec_url)
if validator is not None:
warnings.warn(
"validator parameter is deprecated. Use cls instead.",
DeprecationWarning,
)
return validator.validate(spec, base_uri=base_uri, spec_url=spec_url)
if cls is None:
cls = get_validator_cls(spec)
v = cls(spec)
return v.validate()


def validate_spec_url(
spec_url: str,
validator: SupportsValidation = openapi_spec_validator_proxy,
validator: Optional[SupportsValidation] = None,
cls: Optional[Type[SpecValidator]] = None,
) -> None:
spec = all_urls_handler(spec_url)
return validator.validate(spec, base_uri=spec_url)
return validate_spec(spec, base_uri=spec_url, validator=validator, cls=cls)
67 changes: 25 additions & 42 deletions openapi_spec_validator/validation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,49 @@
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
from openapi_schema_validator.validators import OAS31Validator

from openapi_spec_validator.schemas import schema_v2
from openapi_spec_validator.schemas import schema_v30
from openapi_spec_validator.schemas import schema_v31
from openapi_spec_validator.validation.proxies import DetectValidatorProxy
from openapi_spec_validator.validation.validators import SpecValidator
from openapi_spec_validator.validation.proxies import SpecValidatorProxy
from openapi_spec_validator.validation.validators import OpenAPIV2SpecValidator
from openapi_spec_validator.validation.validators import (
OpenAPIV30SpecValidator,
)
from openapi_spec_validator.validation.validators import (
OpenAPIV31SpecValidator,
)

__all__ = [
"openapi_v2_spec_validator",
"openapi_v3_spec_validator",
"openapi_v30_spec_validator",
"openapi_v31_spec_validator",
"openapi_spec_validator_proxy",
"OpenAPIV2SpecValidator",
"OpenAPIV3SpecValidator",
"OpenAPIV30SpecValidator",
"OpenAPIV31SpecValidator",
]

# v2.0 spec
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 = SpecValidatorProxy(
OpenAPIV2SpecValidator,
deprecated="openapi_v2_spec_validator",
use="OpenAPIV2SpecValidator",
)
openapi_v2_spec_validator = Proxy(get_openapi_v2_spec_validator)

# v3.0 spec
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 = SpecValidatorProxy(
OpenAPIV30SpecValidator,
deprecated="openapi_v30_spec_validator",
use="OpenAPIV30SpecValidator",
)
openapi_v30_spec_validator = Proxy(get_openapi_v30_spec_validator)

# v3.1 spec
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 = SpecValidatorProxy(
OpenAPIV31SpecValidator,
deprecated="openapi_v31_spec_validator",
use="OpenAPIV31SpecValidator",
)
openapi_v31_spec_validator = Proxy(get_openapi_v31_spec_validator)

# alias to the latest v3 version
openapi_v3_spec_validator = openapi_v31_spec_validator
OpenAPIV3SpecValidator = OpenAPIV31SpecValidator

# detect version spec
openapi_spec_validator_proxy = DetectValidatorProxy(
Expand Down
65 changes: 65 additions & 0 deletions openapi_spec_validator/validation/caches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from typing import Generic
from typing import Iterable
from typing import Iterator
from typing import List
from typing import TypeVar

T = TypeVar("T")


class CachedIterable(Iterable[T], Generic[T]):
"""
A cache-implementing wrapper for an iterator.
Note that this is class is `Iterable[T]` rather than `Iterator[T]`.
It should not be iterated by his own.
"""

cache: List[T]
iter: Iterator[T]
completed: bool

def __init__(self, it: Iterator[T]):
self.iter = iter(it)
self.cache = list()
self.completed = False

def __iter__(self) -> Iterator[T]:
return CachedIterator(self)

def __next__(self) -> T:
try:
item = next(self.iter)
except StopIteration:
self.completed = True
raise
else:
self.cache.append(item)
return item

def __del__(self) -> None:
del self.cache


class CachedIterator(Iterator[T], Generic[T]):
"""
A cache-using wrapper for an iterator.
This class is only constructed by `CachedIterable` and cannot be used without it.
"""

parent: CachedIterable[T]
position: int

def __init__(self, parent: CachedIterable[T]):
self.parent = parent
self.position = 0

def __next__(self) -> T:
if self.position < len(self.parent.cache):
item = self.parent.cache[self.position]
elif self.parent.completed:
raise StopIteration
else:
item = next(self.parent)

self.position += 1
return item
Loading