Skip to content

Commit 1702c16

Browse files
authored
Merge pull request #388 from p1c2u/refactor/path-finder-find-signature-refactor
path finder find sig refactor
2 parents 18f6b0f + 3862773 commit 1702c16

File tree

5 files changed

+33
-25
lines changed

5 files changed

+33
-25
lines changed

openapi_core/templating/paths/finders.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,27 @@ def __init__(self, spec, base_url=None):
1919
self.spec = spec
2020
self.base_url = base_url
2121

22-
def find(self, request):
23-
paths_iter = self._get_paths_iter(request.full_url_pattern)
22+
def find(self, method, full_url_pattern):
23+
paths_iter = self._get_paths_iter(full_url_pattern)
2424
paths_iter_peek = peekable(paths_iter)
2525

2626
if not paths_iter_peek:
27-
raise PathNotFound(request.full_url_pattern)
27+
raise PathNotFound(full_url_pattern)
2828

29-
operations_iter = self._get_operations_iter(
30-
request.method, paths_iter_peek
31-
)
29+
operations_iter = self._get_operations_iter(method, paths_iter_peek)
3230
operations_iter_peek = peekable(operations_iter)
3331

3432
if not operations_iter_peek:
35-
raise OperationNotFound(request.full_url_pattern, request.method)
33+
raise OperationNotFound(full_url_pattern, method)
3634

3735
servers_iter = self._get_servers_iter(
38-
request.full_url_pattern, operations_iter_peek
36+
full_url_pattern, operations_iter_peek
3937
)
4038

4139
try:
4240
return next(servers_iter)
4341
except StopIteration:
44-
raise ServerNotFound(request.full_url_pattern)
42+
raise ServerNotFound(full_url_pattern)
4543

4644
def _get_paths_iter(self, full_url_pattern):
4745
template_paths = []

openapi_core/validation/request/validators.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ def _get_body_value(self, request_body, request):
176176
class RequestParametersValidator(BaseRequestValidator):
177177
def validate(self, request):
178178
try:
179-
path, operation, _, path_result, _ = self._find_path(request)
179+
path, operation, _, path_result, _ = self._find_path(
180+
request.method, request.full_url_pattern
181+
)
180182
except PathError as exc:
181183
return RequestValidationResult(errors=[exc])
182184

@@ -195,7 +197,9 @@ def validate(self, request):
195197
class RequestBodyValidator(BaseRequestValidator):
196198
def validate(self, request):
197199
try:
198-
_, operation, _, _, _ = self._find_path(request)
200+
_, operation, _, _, _ = self._find_path(
201+
request.method, request.full_url_pattern
202+
)
199203
except PathError as exc:
200204
return RequestValidationResult(errors=[exc])
201205

@@ -210,7 +214,9 @@ def validate(self, request):
210214
class RequestSecurityValidator(BaseRequestValidator):
211215
def validate(self, request):
212216
try:
213-
_, operation, _, _, _ = self._find_path(request)
217+
_, operation, _, _, _ = self._find_path(
218+
request.method, request.full_url_pattern
219+
)
214220
except PathError as exc:
215221
return RequestValidationResult(errors=[exc])
216222

@@ -228,7 +234,9 @@ def validate(self, request):
228234
class RequestValidator(BaseRequestValidator):
229235
def validate(self, request):
230236
try:
231-
path, operation, _, path_result, _ = self._find_path(request)
237+
path, operation, _, path_result, _ = self._find_path(
238+
request.method, request.full_url_pattern
239+
)
232240
# don't process if operation errors
233241
except PathError as exc:
234242
return RequestValidationResult(errors=[exc])

openapi_core/validation/response/validators.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ def schema_unmarshallers_factory(self):
3333
)
3434

3535
def _find_operation_response(self, request, response):
36-
_, operation, _, _, _ = self._find_path(request)
36+
_, operation, _, _, _ = self._find_path(
37+
request.method, request.full_url_pattern
38+
)
3739
return self._get_operation_response(operation, response)
3840

3941
def _get_operation_response(self, operation, response):

openapi_core/validation/validators.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def parameter_deserializers_factory(self):
4848
def schema_unmarshallers_factory(self):
4949
raise NotImplementedError
5050

51-
def _find_path(self, request):
52-
return self.path_finder.find(request)
51+
def _find_path(self, method, full_url_pattern):
52+
return self.path_finder.find(method, full_url_pattern)
5353

5454
def _get_media_type(self, content, mimetype):
5555
from openapi_core.templating.media_types.finders import MediaTypeFinder

tests/unit/templating/test_paths_finders.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def test_raises(self, finder):
185185
request = MockRequest("http://petstore.swagger.io", "get", request_uri)
186186

187187
with pytest.raises(ServerNotFound):
188-
finder.find(request)
188+
finder.find(request.method, request.full_url_pattern)
189189

190190

191191
class BaseTestOperationNotFound:
@@ -198,7 +198,7 @@ def test_raises(self, finder):
198198
request = MockRequest("http://petstore.swagger.io", "get", request_uri)
199199

200200
with pytest.raises(OperationNotFound):
201-
finder.find(request)
201+
finder.find(request.method, request.full_url_pattern)
202202

203203

204204
class BaseTestValid:
@@ -209,7 +209,7 @@ def test_simple(self, finder, spec):
209209
"http://petstore.swagger.io", method, request_uri
210210
)
211211

212-
result = finder.find(request)
212+
result = finder.find(request.method, request.full_url_pattern)
213213

214214
path = spec / "paths" / self.path_name
215215
operation = spec / "paths" / self.path_name / method
@@ -234,7 +234,7 @@ def test_variable(self, finder, spec, version):
234234
"http://petstore.swagger.io", method, request_uri
235235
)
236236

237-
result = finder.find(request)
237+
result = finder.find(request.method, request.full_url_pattern)
238238

239239
path = spec / "paths" / self.path_name
240240
operation = spec / "paths" / self.path_name / method
@@ -259,7 +259,7 @@ def test_path_variable(self, finder, spec, res_id):
259259
"http://petstore.swagger.io", method, request_uri
260260
)
261261

262-
result = finder.find(request)
262+
result = finder.find(request.method, request.full_url_pattern)
263263

264264
path = spec / "paths" / self.path_name
265265
operation = spec / "paths" / self.path_name / method
@@ -285,7 +285,7 @@ def test_raises(self, finder):
285285
request = MockRequest("http://petstore.swagger.io", "get", request_uri)
286286

287287
with pytest.raises(PathNotFound):
288-
finder.find(request)
288+
finder.find(request.method, request.full_url_pattern)
289289

290290

291291
class TestSpecSimpleServerServerNotFound(
@@ -565,7 +565,7 @@ def test_valid(self, finder, spec):
565565
"http://petstore.swagger.io", method, request_uri
566566
)
567567

568-
result = finder.find(request)
568+
result = finder.find(request.method, request.full_url_pattern)
569569

570570
path_2 = spec / "paths" / self.path_2_name
571571
operation_2 = spec / "paths" / self.path_2_name / method
@@ -619,7 +619,7 @@ def test_valid(self, finder, spec):
619619
request = MockRequest(
620620
"http://petstore.swagger.io", method, request_uri
621621
)
622-
result = finder.find(request)
622+
result = finder.find(request.method, request.full_url_pattern)
623623

624624
path_2 = spec / "paths" / self.path_2_name
625625
operation_2 = spec / "paths" / self.path_2_name / method
@@ -674,7 +674,7 @@ def test_valid(self, finder, spec):
674674
request = MockRequest(
675675
"http://petstore.swagger.io", method, request_uri
676676
)
677-
result = finder.find(request)
677+
result = finder.find(request.method, request.full_url_pattern)
678678

679679
path_2 = spec / "paths" / self.path_2_name
680680
operation_2 = spec / "paths" / self.path_2_name / method

0 commit comments

Comments
 (0)