Merge pull request #145 from gweis/path-parameter-override

very simple and hacky path item parameter override test
This commit is contained in:
A 2019-06-20 16:34:03 +01:00 committed by GitHub
commit 0e30b71a77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 14 deletions

View file

@ -1,4 +1,5 @@
"""OpenAPI core validation request validators module"""
from itertools import chain
from six import iteritems
from openapi_core.schema.exceptions import OpenAPIMappingError
@ -28,30 +29,34 @@ class RequestValidator(object):
try:
path = self.spec[operation_pattern]
# don't process if operation errors
except OpenAPIMappingError as exc:
return RequestValidationResult([exc, ], None, None)
path_params, path_params_errors = self._get_parameters(request, path)
try:
operation = self.spec.get_operation(
operation_pattern, request.method)
# don't process if operation errors
except OpenAPIMappingError as exc:
return RequestValidationResult([exc, ], None, None)
op_params, op_params_errors = self._get_parameters(request, operation)
params, params_errors = self._get_parameters(
request, chain(
iteritems(operation.parameters),
iteritems(path.parameters)
)
)
body, body_errors = self._get_body(request, operation)
errors = path_params_errors + op_params_errors + body_errors
return RequestValidationResult(errors, body, path_params + op_params)
errors = params_errors + body_errors
return RequestValidationResult(errors, body, params)
def _get_parameters(self, request, operation):
def _get_parameters(self, request, params):
errors = []
seen = set()
parameters = RequestParameters()
for param_name, param in iteritems(operation.parameters):
for param_name, param in params:
if (param_name, param.location.value) in seen:
# skip parameter already seen
# e.g. overriden path item paremeter on operation
continue
seen.add((param_name, param.location.value))
try:
raw_value = param.get_value(request)
except MissingParameter:

View file

@ -235,7 +235,7 @@ class TestRequestValidator(object):
class TestPathItemParamsValidator(object):
@pytest.fixture
def spec_dict(self, factory):
def spec_dict(self):
return {
"openapi": "3.0.0",
"info": {
@ -305,6 +305,50 @@ class TestPathItemParamsValidator(object):
assert result.body is None
assert result.parameters == {'query': {'resId': 10}}
def test_request_override_param(self, spec_dict):
# override path parameter on operation
spec_dict["paths"]["/resource"]["get"]["parameters"] = [
{
# full valid parameter object required
"name": "resId",
"in": "query",
"required": False,
"schema": {
"type": "integer",
},
}
]
validator = RequestValidator(create_spec(spec_dict))
request = MockRequest('http://example.com', 'get', '/resource')
result = validator.validate(request)
assert len(result.errors) == 0
assert result.body is None
assert result.parameters == {}
def test_request_override_param_uniqueness(self, spec_dict):
# add parameter on operation with same name as on path but
# different location
spec_dict["paths"]["/resource"]["get"]["parameters"] = [
{
# full valid parameter object required
"name": "resId",
"in": "header",
"required": False,
"schema": {
"type": "integer",
},
}
]
validator = RequestValidator(create_spec(spec_dict))
request = MockRequest('http://example.com', 'get', '/resource')
result = validator.validate(request)
assert len(result.errors) == 1
assert type(result.errors[0]) == MissingRequiredParameter
assert result.body is None
assert result.parameters == {}
class TestResponseValidator(object):