Skip to content

Commit

Permalink
RequestValidator and ResponseValidator backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
p1c2u committed Jan 26, 2023
1 parent 99fb081 commit e985598
Show file tree
Hide file tree
Showing 16 changed files with 589 additions and 320 deletions.
32 changes: 20 additions & 12 deletions openapi_core/validation/request/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
DetectSpecRequestValidatorProxy,
)
from openapi_core.validation.request.proxies import SpecRequestValidatorProxy
from openapi_core.validation.request.validators import RequestBodyValidator
from openapi_core.validation.request.validators import (
RequestParametersValidator,
APICallRequestBodyValidator,
)
from openapi_core.validation.request.validators import RequestSecurityValidator
from openapi_core.validation.request.validators import RequestValidator
from openapi_core.validation.request.validators import (
APICallRequestParametersValidator,
)
from openapi_core.validation.request.validators import (
APICallRequestSecurityValidator,
)
from openapi_core.validation.request.validators import APICallRequestValidator
from openapi_core.validation.request.validators import V30RequestBodyValidator
from openapi_core.validation.request.validators import (
V30RequestParametersValidator,
Expand Down Expand Up @@ -85,37 +89,41 @@

# spec validators
openapi_v30_request_body_validator = SpecRequestValidatorProxy(
RequestBodyValidator,
APICallRequestBodyValidator,
schema_unmarshallers_factory=oas30_request_schema_unmarshallers_factory,
)
openapi_v30_request_parameters_validator = SpecRequestValidatorProxy(
RequestParametersValidator,
APICallRequestParametersValidator,
schema_unmarshallers_factory=oas30_request_schema_unmarshallers_factory,
)
openapi_v30_request_security_validator = SpecRequestValidatorProxy(
RequestSecurityValidator,
APICallRequestSecurityValidator,
schema_unmarshallers_factory=oas30_request_schema_unmarshallers_factory,
)
openapi_v30_request_validator = SpecRequestValidatorProxy(
RequestValidator,
APICallRequestValidator,
schema_unmarshallers_factory=oas30_request_schema_unmarshallers_factory,
deprecated="openapi_v30_request_validator",
use="V30RequestValidator",
)

openapi_v31_request_body_validator = SpecRequestValidatorProxy(
RequestBodyValidator,
APICallRequestBodyValidator,
schema_unmarshallers_factory=oas31_schema_unmarshallers_factory,
)
openapi_v31_request_parameters_validator = SpecRequestValidatorProxy(
RequestParametersValidator,
APICallRequestParametersValidator,
schema_unmarshallers_factory=oas31_schema_unmarshallers_factory,
)
openapi_v31_request_security_validator = SpecRequestValidatorProxy(
RequestSecurityValidator,
APICallRequestSecurityValidator,
schema_unmarshallers_factory=oas31_schema_unmarshallers_factory,
)
openapi_v31_request_validator = SpecRequestValidatorProxy(
RequestValidator,
APICallRequestValidator,
schema_unmarshallers_factory=oas31_schema_unmarshallers_factory,
deprecated="openapi_v31_request_validator",
use="V31RequestValidator",
)

# spec validators alias to the latest v3 version
Expand Down
19 changes: 13 additions & 6 deletions openapi_core/validation/request/proxies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""OpenAPI spec validator validation proxies module."""
import warnings
from typing import TYPE_CHECKING
from typing import Any
from typing import Iterator
from typing import Mapping
Expand All @@ -11,29 +12,35 @@
from openapi_core.validation.exceptions import ValidatorDetectError
from openapi_core.validation.request.datatypes import RequestValidationResult
from openapi_core.validation.request.protocols import Request
from openapi_core.validation.request.validators import (
BaseAPICallRequestValidator,
)

if TYPE_CHECKING:
from openapi_core.validation.request.validators import (
BaseAPICallRequestValidator,
)


class SpecRequestValidatorProxy:
def __init__(
self,
validator_cls: Type[BaseAPICallRequestValidator],
validator_cls: Type["BaseAPICallRequestValidator"],
deprecated: str = "RequestValidator",
use: Optional[str] = None,
**validator_kwargs: Any,
):
self.validator_cls = validator_cls
self.validator_kwargs = validator_kwargs

self.deprecated = deprecated
self.use = use or self.validator_cls.__name__

def validate(
self,
spec: Spec,
request: Request,
base_url: Optional[str] = None,
) -> RequestValidationResult:
warnings.warn(
"openapi_request_validator is deprecated. "
f"Use {self.validator_cls.__name__} class instead.",
f"{self.deprecated} is deprecated. Use {self.use} instead.",
DeprecationWarning,
)
validator = self.validator_cls(
Expand Down
43 changes: 29 additions & 14 deletions openapi_core/validation/request/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
from openapi_core.validation.request.protocols import BaseRequest
from openapi_core.validation.request.protocols import Request
from openapi_core.validation.request.protocols import WebhookRequest
from openapi_core.validation.request.proxies import SpecRequestValidatorProxy
from openapi_core.validation.validators import BaseAPICallValidator
from openapi_core.validation.validators import BaseValidator
from openapi_core.validation.validators import BaseWebhookValidator
Expand Down Expand Up @@ -105,7 +106,7 @@ def _validate(
params = self._get_parameters(request.parameters, operation, path)
except ParametersError as exc:
params = exc.parameters
params_errors = exc.context
params_errors = exc.errors
else:
params_errors = []

Expand Down Expand Up @@ -154,7 +155,7 @@ def _validate_parameters(
params = self._get_parameters(request.parameters, path, operation)
except ParametersError as exc:
params = exc.parameters
params_errors = exc.context
params_errors = exc.errors
else:
params_errors = []

Expand Down Expand Up @@ -328,7 +329,7 @@ def validate(self, request: WebhookRequest) -> RequestValidationResult:
raise NotImplementedError


class RequestBodyValidator(BaseAPICallRequestValidator):
class APICallRequestBodyValidator(BaseAPICallRequestValidator):
def validate(self, request: Request) -> RequestValidationResult:
try:
_, operation, _, _, _ = self._find_path(request)
Expand All @@ -338,7 +339,7 @@ def validate(self, request: Request) -> RequestValidationResult:
return self._validate_body(request, operation)


class RequestParametersValidator(BaseAPICallRequestValidator):
class APICallRequestParametersValidator(BaseAPICallRequestValidator):
def validate(self, request: Request) -> RequestValidationResult:
try:
path, operation, _, path_result, _ = self._find_path(request)
Expand All @@ -352,7 +353,7 @@ def validate(self, request: Request) -> RequestValidationResult:
return self._validate_parameters(request, operation, path)


class RequestSecurityValidator(BaseAPICallRequestValidator):
class APICallRequestSecurityValidator(BaseAPICallRequestValidator):
def validate(self, request: Request) -> RequestValidationResult:
try:
_, operation, _, _, _ = self._find_path(request)
Expand All @@ -362,7 +363,7 @@ def validate(self, request: Request) -> RequestValidationResult:
return self._validate_security(request, operation)


class RequestValidator(BaseAPICallRequestValidator):
class APICallRequestValidator(BaseAPICallRequestValidator):
def validate(self, request: Request) -> RequestValidationResult:
try:
path, operation, _, path_result, _ = self._find_path(request)
Expand Down Expand Up @@ -426,35 +427,35 @@ def validate(self, request: WebhookRequest) -> RequestValidationResult:
return self._validate_security(request, operation)


class V30RequestBodyValidator(RequestBodyValidator):
class V30RequestBodyValidator(APICallRequestBodyValidator):
schema_unmarshallers_factory = oas30_request_schema_unmarshallers_factory


class V30RequestParametersValidator(RequestParametersValidator):
class V30RequestParametersValidator(APICallRequestParametersValidator):
schema_unmarshallers_factory = oas30_request_schema_unmarshallers_factory


class V30RequestSecurityValidator(RequestSecurityValidator):
class V30RequestSecurityValidator(APICallRequestSecurityValidator):
schema_unmarshallers_factory = oas30_request_schema_unmarshallers_factory


class V30RequestValidator(RequestValidator):
class V30RequestValidator(APICallRequestValidator):
schema_unmarshallers_factory = oas30_request_schema_unmarshallers_factory


class V31RequestBodyValidator(RequestBodyValidator):
class V31RequestBodyValidator(APICallRequestBodyValidator):
schema_unmarshallers_factory = oas31_schema_unmarshallers_factory


class V31RequestParametersValidator(RequestParametersValidator):
class V31RequestParametersValidator(APICallRequestParametersValidator):
schema_unmarshallers_factory = oas31_schema_unmarshallers_factory


class V31RequestSecurityValidator(RequestSecurityValidator):
class V31RequestSecurityValidator(APICallRequestSecurityValidator):
schema_unmarshallers_factory = oas31_schema_unmarshallers_factory


class V31RequestValidator(RequestValidator):
class V31RequestValidator(APICallRequestValidator):
schema_unmarshallers_factory = oas31_schema_unmarshallers_factory
path_finder_cls = WebhookPathFinder

Expand All @@ -477,3 +478,17 @@ class V31WebhookRequestSecurityValidator(WebhookRequestSecurityValidator):
class V31WebhookRequestValidator(WebhookRequestValidator):
schema_unmarshallers_factory = oas31_schema_unmarshallers_factory
path_finder_cls = WebhookPathFinder


# backward compatibility
class RequestValidator(SpecRequestValidatorProxy):
def __init__(
self,
schema_unmarshallers_factory: SchemaUnmarshallersFactory,
**kwargs: Any,
):
super().__init__(
APICallRequestValidator,
schema_unmarshallers_factory=schema_unmarshallers_factory,
**kwargs,
)
26 changes: 17 additions & 9 deletions openapi_core/validation/response/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
DetectResponseValidatorProxy,
)
from openapi_core.validation.response.proxies import SpecResponseValidatorProxy
from openapi_core.validation.response.validators import ResponseDataValidator
from openapi_core.validation.response.validators import (
ResponseHeadersValidator,
APICallResponseDataValidator,
)
from openapi_core.validation.response.validators import (
APICallResponseHeadersValidator,
)
from openapi_core.validation.response.validators import (
APICallResponseValidator,
)
from openapi_core.validation.response.validators import ResponseValidator
from openapi_core.validation.response.validators import (
V30ResponseDataValidator,
)
Expand Down Expand Up @@ -72,29 +76,33 @@

# spec validators
openapi_v30_response_data_validator = SpecResponseValidatorProxy(
ResponseDataValidator,
APICallResponseDataValidator,
schema_unmarshallers_factory=oas30_response_schema_unmarshallers_factory,
)
openapi_v30_response_headers_validator = SpecResponseValidatorProxy(
ResponseHeadersValidator,
APICallResponseHeadersValidator,
schema_unmarshallers_factory=oas30_response_schema_unmarshallers_factory,
)
openapi_v30_response_validator = SpecResponseValidatorProxy(
ResponseValidator,
APICallResponseValidator,
schema_unmarshallers_factory=oas30_response_schema_unmarshallers_factory,
deprecated="openapi_v30_response_validator",
use="V30ResponseValidator",
)

openapi_v31_response_data_validator = SpecResponseValidatorProxy(
ResponseDataValidator,
APICallResponseDataValidator,
schema_unmarshallers_factory=oas31_schema_unmarshallers_factory,
)
openapi_v31_response_headers_validator = SpecResponseValidatorProxy(
ResponseHeadersValidator,
APICallResponseHeadersValidator,
schema_unmarshallers_factory=oas31_schema_unmarshallers_factory,
)
openapi_v31_response_validator = SpecResponseValidatorProxy(
ResponseValidator,
APICallResponseValidator,
schema_unmarshallers_factory=oas31_schema_unmarshallers_factory,
deprecated="openapi_v31_response_validator",
use="V31ResponseValidator",
)

# spec validators alias to the latest v3 version
Expand Down
19 changes: 13 additions & 6 deletions openapi_core/validation/response/proxies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""OpenAPI spec validator validation proxies module."""
import warnings
from typing import TYPE_CHECKING
from typing import Any
from typing import Iterator
from typing import Mapping
Expand All @@ -12,20 +13,27 @@
from openapi_core.validation.request.protocols import Request
from openapi_core.validation.response.datatypes import ResponseValidationResult
from openapi_core.validation.response.protocols import Response
from openapi_core.validation.response.validators import (
BaseAPICallResponseValidator,
)

if TYPE_CHECKING:
from openapi_core.validation.response.validators import (
BaseAPICallResponseValidator,
)


class SpecResponseValidatorProxy:
def __init__(
self,
validator_cls: Type[BaseAPICallResponseValidator],
validator_cls: Type["BaseAPICallResponseValidator"],
deprecated: str = "ResponseValidator",
use: Optional[str] = None,
**validator_kwargs: Any,
):
self.validator_cls = validator_cls
self.validator_kwargs = validator_kwargs

self.deprecated = deprecated
self.use = use or self.validator_cls.__name__

def validate(
self,
spec: Spec,
Expand All @@ -34,8 +42,7 @@ def validate(
base_url: Optional[str] = None,
) -> ResponseValidationResult:
warnings.warn(
"openapi_response_validator is deprecated. "
f"Use {self.validator_cls.__name__} class instead.",
f"{self.deprecated} is deprecated. Use {self.use} instead.",
DeprecationWarning,
)
validator = self.validator_cls(
Expand Down
Loading

0 comments on commit e985598

Please sign in to comment.