<commit_before>"""OpenAPI core validation request validators module"""
from itertools import chain
from six import iteritems

from openapi_core.casting.schemas.exceptions import CastError
from openapi_core.deserializing.exceptions import DeserializeError
from openapi_core.schema.media_types.exceptions import InvalidContentType
from openapi_core.schema.operations.exceptions import InvalidOperation
from openapi_core.schema.parameters.exceptions import (
    MissingRequiredParameter, MissingParameter,
)
from openapi_core.schema.paths.exceptions import InvalidPath
from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
from openapi_core.schema.servers.exceptions import InvalidServer
from openapi_core.security.exceptions import SecurityError
from openapi_core.unmarshalling.schemas.enums import UnmarshalContext
from openapi_core.unmarshalling.schemas.exceptions import (
    UnmarshalError, ValidateError,
)
from openapi_core.validation.exceptions import InvalidSecurity
from openapi_core.validation.request.datatypes import (
    RequestParameters, RequestValidationResult,
)
from openapi_core.validation.util import get_operation_pattern


class RequestValidator(object):

    def __init__(
            self, spec,
            custom_formatters=None, custom_media_type_deserializers=None,
    ):
        self.spec = spec
        self.custom_formatters = custom_formatters
        self.custom_media_type_deserializers = custom_media_type_deserializers

    def validate(self, request):
        try:
            path = self._get_path(request)
            operation = self._get_operation(request)
        # don't process if operation errors
        except (InvalidServer, InvalidPath, InvalidOperation) as exc:
            return RequestValidationResult([exc, ], None, None, None)

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

        params, params_errors = self._get_parameters(
            request, chain(
                iteritems(operation.parameters),
                iteritems(path.parameters)
            )
        )

        body, body_errors = self._get_body(request, operation)

        errors = params_errors + body_errors
        return RequestValidationResult(errors, body, params, security)

    def _validate_parameters(self, request):
        try:
            path = self._get_path(request)
            operation = self._get_operation(request)
        except (InvalidServer, InvalidPath, InvalidOperation) as exc:
            return RequestValidationResult([exc, ], None, None)

        params, params_errors = self._get_parameters(
            request, chain(
                iteritems(operation.parameters),
                iteritems(path.parameters)
            )
        )
        return RequestValidationResult(params_errors, None, params, None)

    def _validate_body(self, request):
        try:
            operation = self._get_operation(request)
        except (InvalidServer, InvalidOperation) as exc:
            return RequestValidationResult([exc, ], None, None)

        body, body_errors = self._get_body(request, operation)
        return RequestValidationResult(body_errors, body, None, None)

    def _get_operation_pattern(self, request):
        server = self.spec.get_server(request.full_url_pattern)

        return get_operation_pattern(
            server.default_url, request.full_url_pattern
        )

    def _get_path(self, request):
        operation_pattern = self._get_operation_pattern(request)

        return self.spec[operation_pattern]

    def _get_operation(self, request):
        operation_pattern = self._get_operation_pattern(request)

        return self.spec.get_operation(operation_pattern, request.method)

    def _get_security(self, request, operation):
        security = operation.security or self.spec.security
        if not security:
            return {}

        for security_requirement in security:
            try:
                return {
                    scheme_name: self._get_security_value(
                        scheme_name, request)
                    for scheme_name in security_requirement
                }
            except SecurityError:
                continue

        raise InvalidSecurity()
<commit_msg>Unify find path method of request validator<commit_after>"""OpenAPI core validation request validators module"""
from itertools import chain
from six import iteritems

from openapi_core.casting.schemas.exceptions import CastError
from openapi_core.deserializing.exceptions import DeserializeError
from openapi_core.schema.media_types.exceptions import InvalidContentType
from openapi_core.schema.operations.exceptions import InvalidOperation
from openapi_core.schema.parameters.exceptions import (
    MissingRequiredParameter, MissingParameter,
)
from openapi_core.schema.paths.exceptions import InvalidPath
from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
from openapi_core.schema.servers.exceptions import InvalidServer
from openapi_core.security.exceptions import SecurityError
from openapi_core.unmarshalling.schemas.enums import UnmarshalContext
from openapi_core.unmarshalling.schemas.exceptions import (
    UnmarshalError, ValidateError,
)
from openapi_core.validation.exceptions import InvalidSecurity
from openapi_core.validation.request.datatypes import (
    RequestParameters, RequestValidationResult,
)
from openapi_core.validation.util import get_operation_pattern


class RequestValidator(object):

    def __init__(
            self, spec,
            custom_formatters=None, custom_media_type_deserializers=None,
    ):
        self.spec = spec
        self.custom_formatters = custom_formatters
        self.custom_media_type_deserializers = custom_media_type_deserializers

    def validate(self, request):
        try:
            path, operation, _, _, _ = self._find_path(request)
        # don't process if operation errors
        except (InvalidServer, InvalidPath, InvalidOperation) as exc:
            return RequestValidationResult([exc, ], None, None, None)

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

        params, params_errors = self._get_parameters(
            request, chain(
                iteritems(operation.parameters),
                iteritems(path.parameters)
            )
        )

        body, body_errors = self._get_body(request, operation)

        errors = params_errors + body_errors
        return RequestValidationResult(errors, body, params, security)

    def _validate_parameters(self, request):
        try:
            path, operation, _, _, _ = self._find_path(request)
        except (InvalidServer, InvalidPath, InvalidOperation) as exc:
            return RequestValidationResult([exc, ], None, None)

        params, params_errors = self._get_parameters(
            request, chain(
                iteritems(operation.parameters),
                iteritems(path.parameters)
            )
        )
        return RequestValidationResult(params_errors, None, params, None)

    def _validate_body(self, request):
        try:
            _, operation, _, _, _ = self._find_path(request)
        except (InvalidServer, InvalidOperation) as exc:
            return RequestValidationResult([exc, ], None, None)

        body, body_errors = self._get_body(request, operation)
        return RequestValidationResult(body_errors, body, None, None)

    def _get_operation_pattern(self, request):
        server = self.spec.get_server(request.full_url_pattern)

        return get_operation_pattern(
            server.default_url, request.full_url_pattern
        )

    def _find_path(self, request):
        operation_pattern = self._get_operation_pattern(request)

        path = self.spec[operation_pattern]
        path_variables = {}
        operation = self.spec.get_operation(operation_pattern, request.method)
        servers = path.servers or operation.servers or self.spec.servers
        server = servers[0]
        server_variables = {}

        return path, operation, server, path_variables, server_variables

    def _get_security(self, request, operation):
        security = operation.security or self.spec.security
        if not security:
            return {}

        for security_requirement in security:
            try:
                return {
                    scheme_name: self._get_security_value(
                        scheme_name, request)
                    for scheme_name in security_requirement
                }
            except SecurityError:
                continue

        raise InvalidSecurity()

{'fraction_non_alphanumeric': 0.06690021471352695, 'fraction_numerical': 0.00011300711944852526, 'mean_word_length': 2.9211342490031016, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7650592', 'n_tokens_mistral': 2246, 'n_tokens_neox': 2174, 'n_words': 519}