Skip to content

Commit

Permalink
Request validation parameters dataclass
Browse files Browse the repository at this point in the history
  • Loading branch information
p1c2u committed May 29, 2021
1 parent 4a7f88b commit 80889c6
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 65 deletions.
10 changes: 9 additions & 1 deletion openapi_core/validation/request/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,16 @@ class OpenAPIRequest:
parameters: RequestParameters = field(default_factory=RequestParameters)


@dataclass
class Parameters:
query: Dict = field(default_factory=dict)
header: Dict = field(default_factory=dict)
cookie: Dict = field(default_factory=dict)
path: Dict = field(default_factory=dict)


@dataclass
class RequestValidationResult(BaseValidationResult):
body: Optional[str] = None
parameters: RequestParameters = field(default_factory=RequestParameters)
parameters: Parameters = field(default_factory=Parameters)
security: Optional[Dict[str, str]] = None
10 changes: 5 additions & 5 deletions openapi_core/validation/request/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
)
from openapi_core.validation.exceptions import InvalidSecurity
from openapi_core.validation.request.datatypes import (
RequestParameters, RequestValidationResult,
Parameters, RequestValidationResult,
)
from openapi_core.validation.validators import BaseValidator

Expand All @@ -47,7 +47,7 @@ def _get_parameters(self, request, path, operation):

errors = []
seen = set()
locations = {}
parameters = Parameters()
params_iter = iter_params(operation_params, path_params)
for param in params_iter:
param_name = param['name']
Expand All @@ -68,10 +68,10 @@ def _get_parameters(self, request, path, operation):
errors.append(exc)
continue
else:
locations.setdefault(param_location, {})
locations[param_location][param_name] = value
location = getattr(parameters, param_location)
location[param_name] = value

return RequestParameters(**locations), errors
return parameters, errors

def _get_parameter(self, param, request):
name = param['name']
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/contrib/falcon/test_falcon_middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from openapi_core.contrib.falcon.middlewares import FalconOpenAPIMiddleware
from openapi_core.shortcuts import create_spec
from openapi_core.validation.request.datatypes import RequestParameters
from openapi_core.validation.request.datatypes import Parameters


class TestFalconOpenAPIMiddleware:
Expand Down Expand Up @@ -62,7 +62,7 @@ def view_response_callable(request, response, id):
from falcon.status_codes import HTTP_200
assert request.openapi
assert not request.openapi.errors
assert request.openapi.parameters == RequestParameters(path={
assert request.openapi.parameters == Parameters(path={
'id': 12,
})
response.content_type = MEDIA_HTML
Expand Down Expand Up @@ -186,7 +186,7 @@ def view_response_callable(request, response, id):
from falcon.status_codes import HTTP_200
assert request.openapi
assert not request.openapi.errors
assert request.openapi.parameters == RequestParameters(path={
assert request.openapi.parameters == Parameters(path={
'id': 12,
})
response.status = HTTP_200
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/contrib/flask/test_flask_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from openapi_core.contrib.flask.decorators import FlaskOpenAPIViewDecorator
from openapi_core.shortcuts import create_spec
from openapi_core.validation.request.datatypes import RequestParameters
from openapi_core.validation.request.datatypes import Parameters


class TestFlaskOpenAPIDecorator:
Expand Down Expand Up @@ -59,7 +59,7 @@ def view_response_callable(*args, **kwargs):
from flask.globals import request
assert request.openapi
assert not request.openapi.errors
assert request.openapi.parameters == RequestParameters(path={
assert request.openapi.parameters == Parameters(path={
'id': 12,
})
resp = make_response('success', 200)
Expand Down Expand Up @@ -171,7 +171,7 @@ def view_response_callable(*args, **kwargs):
from flask.globals import request
assert request.openapi
assert not request.openapi.errors
assert request.openapi.parameters == RequestParameters(path={
assert request.openapi.parameters == Parameters(path={
'id': 12,
})
resp = jsonify(data='data')
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/validation/test_minimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
PathNotFound, OperationNotFound,
)
from openapi_core.testing import MockRequest
from openapi_core.validation.request.datatypes import RequestParameters
from openapi_core.validation.request.datatypes import Parameters
from openapi_core.validation.request.validators import RequestValidator


Expand Down Expand Up @@ -49,7 +49,7 @@ def test_invalid_operation(self, factory, server, spec_path):
assert len(result.errors) == 1
assert isinstance(result.errors[0], OperationNotFound)
assert result.body is None
assert result.parameters == RequestParameters()
assert result.parameters == Parameters()

@pytest.mark.parametrize("server", servers)
@pytest.mark.parametrize("spec_path", spec_paths)
Expand All @@ -64,4 +64,4 @@ def test_invalid_path(self, factory, server, spec_path):
assert len(result.errors) == 1
assert isinstance(result.errors[0], PathNotFound)
assert result.body is None
assert result.parameters == RequestParameters()
assert result.parameters == Parameters()
Loading

0 comments on commit 80889c6

Please sign in to comment.