Skip to content

Commit 96e103a

Browse files
authored
Merge pull request #543 from python-openapi/fix/paths-not-found-fix
paths not found fix
2 parents 9247f47 + 5e24525 commit 96e103a

File tree

4 files changed

+91
-4
lines changed

4 files changed

+91
-4
lines changed

openapi_core/spec/paths.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,14 @@ def from_dict(
6262
ref_resolver_handlers=ref_resolver_handlers,
6363
separator=separator,
6464
)
65+
66+
def exists(self) -> bool:
67+
try:
68+
self.content()
69+
except KeyError:
70+
return False
71+
else:
72+
return True
73+
74+
def uri(self) -> str:
75+
return f"#/{str(self)}"

openapi_core/templating/paths/exceptions.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@ class PathError(OpenAPIError):
99

1010
@dataclass
1111
class PathNotFound(PathError):
12-
"""Find path error"""
12+
"""Path not found"""
1313

1414
url: str
1515

1616
def __str__(self) -> str:
1717
return f"Path not found for {self.url}"
1818

1919

20+
@dataclass
21+
class PathsNotFound(PathNotFound):
22+
"""Paths not found"""
23+
24+
def __str__(self) -> str:
25+
return f"Paths not found in spec: {self.url}"
26+
27+
2028
@dataclass
2129
class OperationNotFound(PathError):
2230
"""Find path operation error"""

openapi_core/templating/paths/finders.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from openapi_core.templating.paths.datatypes import PathOperationServer
1616
from openapi_core.templating.paths.exceptions import OperationNotFound
1717
from openapi_core.templating.paths.exceptions import PathNotFound
18+
from openapi_core.templating.paths.exceptions import PathsNotFound
1819
from openapi_core.templating.paths.exceptions import ServerNotFound
1920
from openapi_core.templating.paths.util import template_path_len
2021
from openapi_core.templating.util import parse
@@ -73,8 +74,10 @@ def __init__(self, spec: Spec, base_url: Optional[str] = None):
7374
self.base_url = base_url
7475

7576
def _get_paths_iter(self, name: str) -> Iterator[Path]:
76-
template_paths: List[Path] = []
7777
paths = self.spec / "paths"
78+
if not paths.exists():
79+
raise PathsNotFound(paths.uri())
80+
template_paths: List[Path] = []
7881
for path_pattern, path in list(paths.items()):
7982
# simple path.
8083
# Return right away since it is always the most concrete
@@ -140,9 +143,9 @@ def _get_servers_iter(
140143

141144
class WebhookPathFinder(BasePathFinder):
142145
def _get_paths_iter(self, name: str) -> Iterator[Path]:
143-
if "webhooks" not in self.spec:
144-
raise PathNotFound("Webhooks not found")
145146
webhooks = self.spec / "webhooks"
147+
if not webhooks.exists():
148+
raise PathsNotFound(webhooks.uri())
146149
for webhook_name, path in list(webhooks.items()):
147150
if name == webhook_name:
148151
path_result = TemplateResult(webhook_name, {})

tests/unit/templating/test_paths_finders.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from openapi_core.templating.datatypes import TemplateResult
55
from openapi_core.templating.paths.exceptions import OperationNotFound
66
from openapi_core.templating.paths.exceptions import PathNotFound
7+
from openapi_core.templating.paths.exceptions import PathsNotFound
78
from openapi_core.templating.paths.exceptions import ServerNotFound
89
from openapi_core.templating.paths.finders import APICallPathFinder
910
from openapi_core.testing import MockRequest
@@ -272,6 +273,22 @@ def test_raises(self, finder):
272273
finder.find(method, full_url)
273274

274275

276+
class BaseTestPathsNotFound:
277+
@pytest.fixture
278+
def spec(self, info):
279+
spec = {
280+
"info": info,
281+
}
282+
return Spec.from_dict(spec, validator=None)
283+
284+
def test_raises(self, finder):
285+
method = "get"
286+
full_url = "http://petstore.swagger.io/resource"
287+
288+
with pytest.raises(PathsNotFound):
289+
finder.find(method, full_url)
290+
291+
275292
class TestSpecSimpleServerServerNotFound(
276293
BaseTestServerNotFound,
277294
BaseTestSpecServer,
@@ -299,6 +316,14 @@ class TestSpecSimpleServerPathNotFound(
299316
pass
300317

301318

319+
class TestSpecSimpleServerPathsNotFound(
320+
BaseTestPathsNotFound,
321+
BaseTestSpecServer,
322+
BaseTestSimpleServer,
323+
):
324+
pass
325+
326+
302327
class TestOperationSimpleServerServerNotFound(
303328
BaseTestServerNotFound,
304329
BaseTestOperationServer,
@@ -326,6 +351,14 @@ class TestOperationSimpleServerPathNotFound(
326351
pass
327352

328353

354+
class TestOperationSimpleServerPathsNotFound(
355+
BaseTestPathsNotFound,
356+
BaseTestOperationServer,
357+
BaseTestSimpleServer,
358+
):
359+
pass
360+
361+
329362
class TestPathSimpleServerServerNotFound(
330363
BaseTestServerNotFound,
331364
BaseTestPathServer,
@@ -353,6 +386,14 @@ class TestPathSimpleServerPathNotFound(
353386
pass
354387

355388

389+
class TestPathSimpleServerPathsNotFound(
390+
BaseTestPathsNotFound,
391+
BaseTestPathServer,
392+
BaseTestSimpleServer,
393+
):
394+
pass
395+
396+
356397
class TestSpecSimpleServerValid(
357398
BaseTestValid, BaseTestSpecServer, BaseTestSimplePath, BaseTestSimpleServer
358399
):
@@ -428,6 +469,14 @@ class TestSpecVariableServerPathNotFound(
428469
pass
429470

430471

472+
class TestSpecVariableServerPathsNotFound(
473+
BaseTestPathsNotFound,
474+
BaseTestSpecServer,
475+
BaseTestVariableServer,
476+
):
477+
pass
478+
479+
431480
class TestOperationVariableServerServerNotFound(
432481
BaseTestServerNotFound,
433482
BaseTestOperationServer,
@@ -455,6 +504,14 @@ class TestOperationVariableServerPathNotFound(
455504
pass
456505

457506

507+
class TestOperationVariableServerPathsNotFound(
508+
BaseTestPathsNotFound,
509+
BaseTestOperationServer,
510+
BaseTestVariableServer,
511+
):
512+
pass
513+
514+
458515
class TestPathVariableServerServerNotFound(
459516
BaseTestServerNotFound,
460517
BaseTestPathServer,
@@ -482,6 +539,14 @@ class TestPathVariableServerPathNotFound(
482539
pass
483540

484541

542+
class TestPathVariableServerPathsNotFound(
543+
BaseTestPathsNotFound,
544+
BaseTestPathServer,
545+
BaseTestVariableServer,
546+
):
547+
pass
548+
549+
485550
class TestSpecVariableServerValid(
486551
BaseTestVariableValid,
487552
BaseTestSpecServer,

0 commit comments

Comments
 (0)