Skip to content

path finder find sig refactor #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions openapi_core/templating/paths/finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
16 changes: 12 additions & 4 deletions openapi_core/validation/request/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand All @@ -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])

Expand All @@ -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])

Expand All @@ -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])
Expand Down
4 changes: 3 additions & 1 deletion openapi_core/validation/response/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions openapi_core/validation/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/templating/test_paths_finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down