Skip to content

Customization refactor #412

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
Sep 7, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions docs/customizations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ By default, spec dict is validated on spec creation time. Disabling the validati
Deserializers
-------------

Pass custom defined media type deserializers dictionary with supported mimetypes as a key to `RequestValidator` or `ResponseValidator` constructor:
Pass custom defined media type deserializers dictionary with supported mimetypes as a key to `MediaTypeDeserializersFactory` and then pass it to `RequestValidator` or `ResponseValidator` constructor:

.. code-block:: python

from openapi_core.deserializing.media_types.factories import MediaTypeDeserializersFactory
from openapi_core.unmarshalling.schemas import oas30_response_schema_unmarshallers_factory

def protobuf_deserializer(message):
feature = route_guide_pb2.Feature()
feature.ParseFromString(message)
Expand All @@ -27,9 +30,14 @@ Pass custom defined media type deserializers dictionary with supported mimetypes
custom_media_type_deserializers = {
'application/protobuf': protobuf_deserializer,
}
media_type_deserializers_factory = MediaTypeDeserializersFactory(
custom_deserializers=custom_media_type_deserializers,
)

validator = ResponseValidator(
custom_media_type_deserializers=custom_media_type_deserializers)
oas30_response_schema_unmarshallers_factory,
media_type_deserializers_factory=media_type_deserializers_factory,
)

result = validator.validate(spec, request, response)

Expand All @@ -38,28 +46,34 @@ Formats

OpenAPI defines a ``format`` keyword that hints at how a value should be interpreted, e.g. a ``string`` with the type ``date`` should conform to the RFC 3339 date format.

Openapi-core comes with a set of built-in formatters, but it's also possible to add support for custom formatters for `RequestValidator` and `ResponseValidator`.
Openapi-core comes with a set of built-in formatters, but it's also possible to add custom formatters in `SchemaUnmarshallersFactory` and pass it to `RequestValidator` or `ResponseValidator`.

Here's how you could add support for a ``usdate`` format that handles dates of the form MM/DD/YYYY:

.. code-block:: python

from datetime import datetime
import re
from openapi_core.unmarshalling.schemas.factories import SchemaUnmarshallersFactory
from openapi_schema_validator import OAS30Validator
from datetime import datetime
import re

class USDateFormatter:
def validate(self, value) -> bool:
return bool(re.match(r"^\d{1,2}/\d{1,2}/\d{4}$", value))
class USDateFormatter:
def validate(self, value) -> bool:
return bool(re.match(r"^\d{1,2}/\d{1,2}/\d{4}$", value))

def unmarshal(self, value):
return datetime.strptime(value, "%m/%d/%y").date
def unmarshal(self, value):
return datetime.strptime(value, "%m/%d/%y").date


custom_formatters = {
'usdate': USDateFormatter(),
}

validator = ResponseValidator(custom_formatters=custom_formatters)
schema_unmarshallers_factory = SchemaUnmarshallersFactory(
OAS30Validator,
custom_formatters=custom_formatters,
context=UnmarshalContext.RESPONSE,
)
validator = ResponseValidator(schema_unmarshallers_factory)

result = validator.validate(spec, request, response)

4 changes: 2 additions & 2 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Now you can use it to validate against requests

from openapi_core.validation.request import openapi_request_validator

result = validator.validate(spec, request)
result = openapi_request_validator.validate(spec, request)

# raise errors if request invalid
result.raise_for_errors()
Expand Down Expand Up @@ -57,7 +57,7 @@ You can also validate against responses

from openapi_core.validation.response import openapi_response_validator

result = validator.validate(spec, request, response)
result = openapi_response_validator.validate(spec, request, response)

# raise errors if response invalid
result.raise_for_errors()
Expand Down
5 changes: 5 additions & 0 deletions openapi_core/casting/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from openapi_core.casting.schemas.factories import SchemaCastersFactory

__all__ = ["schema_casters_factory"]

schema_casters_factory = SchemaCastersFactory()
7 changes: 7 additions & 0 deletions openapi_core/deserializing/media_types/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from openapi_core.deserializing.media_types.factories import (
MediaTypeDeserializersFactory,
)

__all__ = ["media_type_deserializers_factory"]

media_type_deserializers_factory = MediaTypeDeserializersFactory()
7 changes: 7 additions & 0 deletions openapi_core/deserializing/parameters/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from openapi_core.deserializing.parameters.factories import (
ParameterDeserializersFactory,
)

__all__ = ["parameter_deserializers_factory"]

parameter_deserializers_factory = ParameterDeserializersFactory()
5 changes: 5 additions & 0 deletions openapi_core/security/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from openapi_core.security.factories import SecurityProviderFactory

__all__ = ["security_provider_factory"]

security_provider_factory = SecurityProviderFactory()
21 changes: 21 additions & 0 deletions openapi_core/unmarshalling/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from openapi_schema_validator import OAS30Validator

from openapi_core.unmarshalling.schemas.enums import UnmarshalContext
from openapi_core.unmarshalling.schemas.factories import (
SchemaUnmarshallersFactory,
)

__all__ = [
"oas30_request_schema_unmarshallers_factory",
"oas30_response_schema_unmarshallers_factory",
]

oas30_request_schema_unmarshallers_factory = SchemaUnmarshallersFactory(
OAS30Validator,
context=UnmarshalContext.REQUEST,
)

oas30_response_schema_unmarshallers_factory = SchemaUnmarshallersFactory(
OAS30Validator,
context=UnmarshalContext.RESPONSE,
)
15 changes: 8 additions & 7 deletions openapi_core/unmarshalling/schemas/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from openapi_core.unmarshalling.schemas.unmarshallers import NumberUnmarshaller
from openapi_core.unmarshalling.schemas.unmarshallers import ObjectUnmarshaller
from openapi_core.unmarshalling.schemas.unmarshallers import StringUnmarshaller
from openapi_core.unmarshalling.schemas.util import build_format_checker


class SchemaUnmarshallersFactory:
Expand All @@ -40,13 +41,11 @@ class SchemaUnmarshallersFactory:

def __init__(
self,
resolver=None,
format_checker=None,
schema_validator_class,
custom_formatters=None,
context=None,
):
self.resolver = resolver
self.format_checker = format_checker
self.schema_validator_class = schema_validator_class
if custom_formatters is None:
custom_formatters = {}
self.custom_formatters = custom_formatters
Expand Down Expand Up @@ -86,11 +85,13 @@ def get_formatter(self, type_format, default_formatters):
return default_formatters.get(type_format)

def get_validator(self, schema):
resolver = schema.accessor.dereferencer.resolver_manager.resolver
format_checker = build_format_checker(**self.custom_formatters)
kwargs = {
"resolver": self.resolver,
"format_checker": self.format_checker,
"resolver": resolver,
"format_checker": format_checker,
}
if self.context is not None:
kwargs[self.CONTEXT_VALIDATION[self.context]] = True
with schema.open() as schema_dict:
return OAS30Validator(schema_dict, **kwargs)
return self.schema_validator_class(schema_dict, **kwargs)
29 changes: 25 additions & 4 deletions openapi_core/validation/request/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""OpenAPI core validation request module"""
from openapi_core.unmarshalling.schemas import (
oas30_request_schema_unmarshallers_factory,
)
from openapi_core.validation.request.validators import RequestBodyValidator
from openapi_core.validation.request.validators import (
RequestParametersValidator,
Expand All @@ -7,13 +10,31 @@
from openapi_core.validation.request.validators import RequestValidator

__all__ = [
"openapi_v30_request_body_validator",
"openapi_v30_request_parameters_validator",
"openapi_v30_request_security_validator",
"openapi_v30_request_validator",
"openapi_request_body_validator",
"openapi_request_parameters_validator",
"openapi_request_security_validator",
"openapi_request_validator",
]

openapi_request_body_validator = RequestBodyValidator()
openapi_request_parameters_validator = RequestParametersValidator()
openapi_request_security_validator = RequestSecurityValidator()
openapi_request_validator = RequestValidator()
openapi_v30_request_body_validator = RequestBodyValidator(
schema_unmarshallers_factory=oas30_request_schema_unmarshallers_factory,
)
openapi_v30_request_parameters_validator = RequestParametersValidator(
schema_unmarshallers_factory=oas30_request_schema_unmarshallers_factory,
)
openapi_v30_request_security_validator = RequestSecurityValidator(
schema_unmarshallers_factory=oas30_request_schema_unmarshallers_factory,
)
openapi_v30_request_validator = RequestValidator(
schema_unmarshallers_factory=oas30_request_schema_unmarshallers_factory,
)

# alias to the latest v3 version
openapi_request_body_validator = openapi_v30_request_body_validator
openapi_request_parameters_validator = openapi_v30_request_parameters_validator
openapi_request_security_validator = openapi_v30_request_security_validator
openapi_request_validator = openapi_v30_request_validator
83 changes: 46 additions & 37 deletions openapi_core/validation/request/validators.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
"""OpenAPI core validation request validators module"""
import warnings

from openapi_core.casting.schemas import schema_casters_factory
from openapi_core.casting.schemas.exceptions import CastError
from openapi_core.deserializing.exceptions import DeserializeError
from openapi_core.deserializing.media_types import (
media_type_deserializers_factory,
)
from openapi_core.deserializing.parameters import (
parameter_deserializers_factory,
)
from openapi_core.schema.parameters import iter_params
from openapi_core.security import security_provider_factory
from openapi_core.security.exceptions import SecurityError
from openapi_core.security.factories import SecurityProviderFactory
from openapi_core.templating.media_types.exceptions import MediaTypeFinderError
from openapi_core.templating.paths.exceptions import PathError
from openapi_core.unmarshalling.schemas.enums import UnmarshalContext
Expand All @@ -28,6 +35,22 @@


class BaseRequestValidator(BaseValidator):
def __init__(
self,
schema_unmarshallers_factory,
schema_casters_factory=schema_casters_factory,
parameter_deserializers_factory=parameter_deserializers_factory,
media_type_deserializers_factory=media_type_deserializers_factory,
security_provider_factory=security_provider_factory,
):
super().__init__(
schema_unmarshallers_factory,
schema_casters_factory=schema_casters_factory,
parameter_deserializers_factory=parameter_deserializers_factory,
media_type_deserializers_factory=media_type_deserializers_factory,
)
self.security_provider_factory = security_provider_factory

def validate(
self,
spec,
Expand All @@ -36,22 +59,6 @@ def validate(
):
raise NotImplementedError

@property
def schema_unmarshallers_factory(self):
spec_resolver = (
self.spec.accessor.dereferencer.resolver_manager.resolver
)
return SchemaUnmarshallersFactory(
spec_resolver,
self.format_checker,
self.custom_formatters,
context=UnmarshalContext.REQUEST,
)

@property
def security_provider_factory(self):
return SecurityProviderFactory()

def _get_parameters(self, request, path, operation):
operation_params = operation.get("parameters", [])
path_params = path.get("parameters", [])
Expand Down Expand Up @@ -109,10 +116,10 @@ def _get_parameter(self, param, request):
raise MissingRequiredParameter(name)
raise MissingParameter(name)

def _get_security(self, request, operation):
def _get_security(self, spec, request, operation):
security = None
if "security" in self.spec:
security = self.spec / "security"
if "security" in spec:
security = spec / "security"
if "security" in operation:
security = operation / "security"

Expand All @@ -122,16 +129,18 @@ def _get_security(self, request, operation):
for security_requirement in security:
try:
return {
scheme_name: self._get_security_value(scheme_name, request)
scheme_name: self._get_security_value(
spec, scheme_name, request
)
for scheme_name in list(security_requirement.keys())
}
except SecurityError:
continue

raise InvalidSecurity

def _get_security_value(self, scheme_name, request):
security_schemes = self.spec / "components#securitySchemes"
def _get_security_value(self, spec, scheme_name, request):
security_schemes = spec / "components#securitySchemes"
if scheme_name not in security_schemes:
return
scheme = security_schemes[scheme_name]
Expand Down Expand Up @@ -174,10 +183,10 @@ def validate(
request,
base_url=None,
):
self.spec = spec
self.base_url = base_url
try:
path, operation, _, path_result, _ = self._find_path(request)
path, operation, _, path_result, _ = self._find_path(
spec, request, base_url=base_url
)
except PathError as exc:
return RequestValidationResult(errors=[exc])

Expand Down Expand Up @@ -206,10 +215,10 @@ def validate(
request,
base_url=None,
):
self.spec = spec
self.base_url = base_url
try:
_, operation, _, _, _ = self._find_path(request)
_, operation, _, _, _ = self._find_path(
spec, request, base_url=base_url
)
except PathError as exc:
return RequestValidationResult(errors=[exc])

Expand Down Expand Up @@ -244,15 +253,15 @@ def validate(
request,
base_url=None,
):
self.spec = spec
self.base_url = base_url
try:
_, operation, _, _, _ = self._find_path(request)
_, operation, _, _, _ = self._find_path(
spec, request, base_url=base_url
)
except PathError as exc:
return RequestValidationResult(errors=[exc])

try:
security = self._get_security(request, operation)
security = self._get_security(spec, request, operation)
except InvalidSecurity as exc:
return RequestValidationResult(errors=[exc])

Expand All @@ -269,16 +278,16 @@ def validate(
request,
base_url=None,
):
self.spec = spec
self.base_url = base_url
try:
path, operation, _, path_result, _ = self._find_path(request)
path, operation, _, path_result, _ = self._find_path(
spec, request, base_url=base_url
)
# don't process if operation errors
except PathError as exc:
return RequestValidationResult(errors=[exc])

try:
security = self._get_security(request, operation)
security = self._get_security(spec, request, operation)
except InvalidSecurity as exc:
return RequestValidationResult(errors=[exc])

Expand Down
Loading