From 3862773b59e37efe81e6221622ce3ca766536c96 Mon Sep 17 00:00:00 2001 From: p1c2u Date: Mon, 31 Jan 2022 11:27:24 +0000 Subject: [PATCH] path finder find sig refactor --- openapi_core/templating/paths/finders.py | 16 +++++++--------- openapi_core/validation/request/validators.py | 16 ++++++++++++---- openapi_core/validation/response/validators.py | 4 +++- openapi_core/validation/validators.py | 4 ++-- tests/unit/templating/test_paths_finders.py | 18 +++++++++--------- 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/openapi_core/templating/paths/finders.py b/openapi_core/templating/paths/finders.py index bdeec72b..0fcd427f 100644 --- a/openapi_core/templating/paths/finders.py +++ b/openapi_core/templating/paths/finders.py @@ -19,29 +19,27 @@ def __init__(self, spec, base_url=None): self.spec = spec self.base_url = base_url - def find(self, request): - paths_iter = self._get_paths_iter(request.full_url_pattern) + def find(self, method, full_url_pattern): + paths_iter = self._get_paths_iter(full_url_pattern) paths_iter_peek = peekable(paths_iter) if not paths_iter_peek: - raise PathNotFound(request.full_url_pattern) + raise PathNotFound(full_url_pattern) - operations_iter = self._get_operations_iter( - request.method, paths_iter_peek - ) + operations_iter = self._get_operations_iter(method, paths_iter_peek) operations_iter_peek = peekable(operations_iter) if not operations_iter_peek: - raise OperationNotFound(request.full_url_pattern, request.method) + raise OperationNotFound(full_url_pattern, method) servers_iter = self._get_servers_iter( - request.full_url_pattern, operations_iter_peek + full_url_pattern, operations_iter_peek ) try: return next(servers_iter) except StopIteration: - raise ServerNotFound(request.full_url_pattern) + raise ServerNotFound(full_url_pattern) def _get_paths_iter(self, full_url_pattern): template_paths = [] diff --git a/openapi_core/validation/request/validators.py b/openapi_core/validation/request/validators.py index 2c42d92d..06d18f42 100644 --- a/openapi_core/validation/request/validators.py +++ b/openapi_core/validation/request/validators.py @@ -176,7 +176,9 @@ def _get_body_value(self, request_body, request): class RequestParametersValidator(BaseRequestValidator): def validate(self, request): try: - path, operation, _, path_result, _ = self._find_path(request) + path, operation, _, path_result, _ = self._find_path( + request.method, request.full_url_pattern + ) except PathError as exc: return RequestValidationResult(errors=[exc]) @@ -195,7 +197,9 @@ def validate(self, request): class RequestBodyValidator(BaseRequestValidator): def validate(self, request): try: - _, operation, _, _, _ = self._find_path(request) + _, operation, _, _, _ = self._find_path( + request.method, request.full_url_pattern + ) except PathError as exc: return RequestValidationResult(errors=[exc]) @@ -210,7 +214,9 @@ def validate(self, request): class RequestSecurityValidator(BaseRequestValidator): def validate(self, request): try: - _, operation, _, _, _ = self._find_path(request) + _, operation, _, _, _ = self._find_path( + request.method, request.full_url_pattern + ) except PathError as exc: return RequestValidationResult(errors=[exc]) @@ -228,7 +234,9 @@ def validate(self, request): class RequestValidator(BaseRequestValidator): def validate(self, request): try: - path, operation, _, path_result, _ = self._find_path(request) + path, operation, _, path_result, _ = self._find_path( + request.method, request.full_url_pattern + ) # don't process if operation errors except PathError as exc: return RequestValidationResult(errors=[exc]) diff --git a/openapi_core/validation/response/validators.py b/openapi_core/validation/response/validators.py index 17804cf4..94b2da91 100644 --- a/openapi_core/validation/response/validators.py +++ b/openapi_core/validation/response/validators.py @@ -33,7 +33,9 @@ def schema_unmarshallers_factory(self): ) def _find_operation_response(self, request, response): - _, operation, _, _, _ = self._find_path(request) + _, operation, _, _, _ = self._find_path( + request.method, request.full_url_pattern + ) return self._get_operation_response(operation, response) def _get_operation_response(self, operation, response): diff --git a/openapi_core/validation/validators.py b/openapi_core/validation/validators.py index 87cf7979..97a8e8f5 100644 --- a/openapi_core/validation/validators.py +++ b/openapi_core/validation/validators.py @@ -48,8 +48,8 @@ def parameter_deserializers_factory(self): def schema_unmarshallers_factory(self): raise NotImplementedError - def _find_path(self, request): - return self.path_finder.find(request) + def _find_path(self, method, full_url_pattern): + return self.path_finder.find(method, full_url_pattern) def _get_media_type(self, content, mimetype): from openapi_core.templating.media_types.finders import MediaTypeFinder diff --git a/tests/unit/templating/test_paths_finders.py b/tests/unit/templating/test_paths_finders.py index cfbc68d0..133b0b20 100644 --- a/tests/unit/templating/test_paths_finders.py +++ b/tests/unit/templating/test_paths_finders.py @@ -185,7 +185,7 @@ def test_raises(self, finder): request = MockRequest("http://petstore.swagger.io", "get", request_uri) with pytest.raises(ServerNotFound): - finder.find(request) + finder.find(request.method, request.full_url_pattern) class BaseTestOperationNotFound: @@ -198,7 +198,7 @@ def test_raises(self, finder): request = MockRequest("http://petstore.swagger.io", "get", request_uri) with pytest.raises(OperationNotFound): - finder.find(request) + finder.find(request.method, request.full_url_pattern) class BaseTestValid: @@ -209,7 +209,7 @@ def test_simple(self, finder, spec): "http://petstore.swagger.io", method, request_uri ) - result = finder.find(request) + result = finder.find(request.method, request.full_url_pattern) path = spec / "paths" / self.path_name operation = spec / "paths" / self.path_name / method @@ -234,7 +234,7 @@ def test_variable(self, finder, spec, version): "http://petstore.swagger.io", method, request_uri ) - result = finder.find(request) + result = finder.find(request.method, request.full_url_pattern) path = spec / "paths" / self.path_name operation = spec / "paths" / self.path_name / method @@ -259,7 +259,7 @@ def test_path_variable(self, finder, spec, res_id): "http://petstore.swagger.io", method, request_uri ) - result = finder.find(request) + result = finder.find(request.method, request.full_url_pattern) path = spec / "paths" / self.path_name operation = spec / "paths" / self.path_name / method @@ -285,7 +285,7 @@ def test_raises(self, finder): request = MockRequest("http://petstore.swagger.io", "get", request_uri) with pytest.raises(PathNotFound): - finder.find(request) + finder.find(request.method, request.full_url_pattern) class TestSpecSimpleServerServerNotFound( @@ -565,7 +565,7 @@ def test_valid(self, finder, spec): "http://petstore.swagger.io", method, request_uri ) - result = finder.find(request) + result = finder.find(request.method, request.full_url_pattern) path_2 = spec / "paths" / self.path_2_name operation_2 = spec / "paths" / self.path_2_name / method @@ -619,7 +619,7 @@ def test_valid(self, finder, spec): request = MockRequest( "http://petstore.swagger.io", method, request_uri ) - result = finder.find(request) + result = finder.find(request.method, request.full_url_pattern) path_2 = spec / "paths" / self.path_2_name operation_2 = spec / "paths" / self.path_2_name / method @@ -674,7 +674,7 @@ def test_valid(self, finder, spec): request = MockRequest( "http://petstore.swagger.io", method, request_uri ) - result = finder.find(request) + result = finder.find(request.method, request.full_url_pattern) path_2 = spec / "paths" / self.path_2_name operation_2 = spec / "paths" / self.path_2_name / method