|
| 1 | +"""OpenAPI core validation request validators module""" |
| 2 | +from six import iteritems |
| 3 | + |
| 4 | +from openapi_core.exceptions import ( |
| 5 | + OpenAPIMappingError, MissingParameter, MissingBody, |
| 6 | +) |
| 7 | +from openapi_core.validation.request.models import ( |
| 8 | + RequestParameters, RequestValidationResult, |
| 9 | +) |
| 10 | +from openapi_core.validation.util import get_operation_pattern |
| 11 | + |
| 12 | + |
| 13 | +class RequestValidator(object): |
| 14 | + |
| 15 | + def __init__(self, spec): |
| 16 | + self.spec = spec |
| 17 | + |
| 18 | + def validate(self, request): |
| 19 | + errors = [] |
| 20 | + body = None |
| 21 | + parameters = RequestParameters() |
| 22 | + |
| 23 | + try: |
| 24 | + server = self.spec.get_server(request.full_url_pattern) |
| 25 | + # don't process if server errors |
| 26 | + except OpenAPIMappingError as exc: |
| 27 | + errors.append(exc) |
| 28 | + return RequestValidationResult(errors, body, parameters) |
| 29 | + |
| 30 | + operation_pattern = get_operation_pattern( |
| 31 | + server.default_url, request.full_url_pattern |
| 32 | + ) |
| 33 | + |
| 34 | + try: |
| 35 | + operation = self.spec.get_operation( |
| 36 | + operation_pattern, request.method) |
| 37 | + # don't process if operation errors |
| 38 | + except OpenAPIMappingError as exc: |
| 39 | + errors.append(exc) |
| 40 | + return RequestValidationResult(errors, body, parameters) |
| 41 | + |
| 42 | + for param_name, param in iteritems(operation.parameters): |
| 43 | + try: |
| 44 | + raw_value = self._get_raw_value(request, param) |
| 45 | + except MissingParameter as exc: |
| 46 | + if param.required: |
| 47 | + errors.append(exc) |
| 48 | + |
| 49 | + if not param.schema or param.schema.default is None: |
| 50 | + continue |
| 51 | + raw_value = param.schema.default |
| 52 | + |
| 53 | + try: |
| 54 | + value = param.unmarshal(raw_value) |
| 55 | + except OpenAPIMappingError as exc: |
| 56 | + errors.append(exc) |
| 57 | + else: |
| 58 | + parameters[param.location.value][param_name] = value |
| 59 | + |
| 60 | + if operation.request_body is not None: |
| 61 | + try: |
| 62 | + media_type = operation.request_body[request.mimetype] |
| 63 | + except OpenAPIMappingError as exc: |
| 64 | + errors.append(exc) |
| 65 | + else: |
| 66 | + try: |
| 67 | + raw_body = self._get_raw_body(request) |
| 68 | + except MissingBody as exc: |
| 69 | + if operation.request_body.required: |
| 70 | + errors.append(exc) |
| 71 | + else: |
| 72 | + try: |
| 73 | + body = media_type.unmarshal(raw_body) |
| 74 | + except OpenAPIMappingError as exc: |
| 75 | + errors.append(exc) |
| 76 | + |
| 77 | + return RequestValidationResult(errors, body, parameters) |
| 78 | + |
| 79 | + def _get_raw_value(self, request, param): |
| 80 | + location = request.parameters[param.location.value] |
| 81 | + |
| 82 | + try: |
| 83 | + raw = location[param.name] |
| 84 | + except KeyError: |
| 85 | + raise MissingParameter( |
| 86 | + "Missing required `{0}` parameter".format(param.name)) |
| 87 | + |
| 88 | + if param.aslist and param.explode: |
| 89 | + return location.getlist(param.name) |
| 90 | + |
| 91 | + return raw |
| 92 | + |
| 93 | + def _get_raw_body(self, request): |
| 94 | + if not request.body: |
| 95 | + raise MissingBody("Missing required request body") |
| 96 | + |
| 97 | + return request.body |
0 commit comments