Skip to content

Commit 7139643

Browse files
committed
Finder exception
1 parent 6d71cad commit 7139643

File tree

13 files changed

+111
-39
lines changed

13 files changed

+111
-39
lines changed

openapi_core/contrib/flask/handlers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
from flask.json import dumps
44

55
from openapi_core.schema.media_types.exceptions import InvalidContentType
6+
from openapi_core.templating.paths.exceptions import (
7+
OperationNotFound, PathNotFound,
8+
)
69
from openapi_core.schema.servers.exceptions import InvalidServer
710

811

912
class FlaskOpenAPIErrorsHandler(object):
1013

1114
OPENAPI_ERROR_STATUS = {
12-
InvalidServer: 500,
15+
OperationNotFound: 500,
16+
PathNotFound: 500,
1317
InvalidContentType: 415,
1418
}
1519

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import attr
2+
3+
from openapi_core.exceptions import OpenAPIError
4+
5+
6+
class PathError(OpenAPIError):
7+
"""Path error"""
8+
9+
10+
@attr.s(hash=True)
11+
class PathNotFound(PathError):
12+
"""Find path error"""
13+
path_pattern = attr.ib()
14+
15+
def __str__(self):
16+
return "Path not found for {0}".format(self.path_pattern)
17+
18+
19+
@attr.s(hash=True)
20+
class OperationNotFound(PathError):
21+
"""Find path operation error"""
22+
path_pattern = attr.ib()
23+
operation = attr.ib()
24+
25+
def __str__(self):
26+
return "Operation {0} not found for {0}".format(
27+
self.operation, self.path_pattern)

openapi_core/templating/paths/finders.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
from parse import parse
22
from six.moves.urllib.parse import urljoin
33

4-
from openapi_core.templating.paths.exceptions import PathNotFound
4+
from openapi_core.schema.operations.exceptions import InvalidOperation
5+
from openapi_core.schema.paths.exceptions import InvalidPath
6+
from openapi_core.schema.servers.exceptions import InvalidServer
7+
from openapi_core.templating.paths.exceptions import (
8+
PathNotFound, OperationNotFound,
9+
)
510
from openapi_core.templating.paths.util import get_operation_pattern
611

712

@@ -11,16 +16,21 @@ def __init__(self, spec):
1116
self.spec = spec
1217

1318
def find(self, request):
14-
operation_pattern = self._get_operation_pattern(request)
15-
16-
path = self.spec[operation_pattern]
17-
path_variables = {}
18-
operation = self.spec.get_operation(operation_pattern, request.method)
19-
servers = path.servers or operation.servers or self.spec.servers
20-
server = servers[0]
21-
server_variables = {}
22-
23-
return path, operation, server, path_variables, server_variables
19+
try:
20+
operation_pattern = self._get_operation_pattern(request)
21+
22+
path = self.spec[operation_pattern]
23+
path_variables = {}
24+
operation = self.spec.get_operation(operation_pattern, request.method)
25+
servers = path.servers or operation.servers or self.spec.servers
26+
server = servers[0]
27+
server_variables = {}
28+
except (InvalidServer, InvalidPath):
29+
raise PathNotFound(request.full_url_pattern)
30+
except InvalidOperation:
31+
raise OperationNotFound(request.full_url_pattern, request.method)
32+
else:
33+
return path, operation, server, path_variables, server_variables
2434

2535
def _get_operation_pattern(self, request):
2636
server = self.spec.get_server(request.full_url_pattern)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from parse import parse
2+
from six.moves.urllib.parse import urljoin
3+
4+
from openapi_core.templating.paths.exceptions import PathNotFound
5+
6+
7+
class PathFinder(object):
8+
9+
def __init__(self, spec):
10+
self.spec = spec
11+
12+
def find(self, request):
13+
for path in self.spec.paths:
14+
if request.method not in path.operations:
15+
continue
16+
operation = path.operations[request.method]
17+
servers = path.servers or operations.servers or self.spec.servers
18+
for server in servers:
19+
path_pattern = urljoin(server.url, path.name)
20+
if request.path_pattern == path_pattern:
21+
return path, operation, server, {}
22+
parsed = parse(path_pattern, request.path_pattern)
23+
if parsed:
24+
return path, operation, server, parsed.named
25+
26+
raise PathNotFound(request.path_pattern)

openapi_core/validation/request/validators.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
1414
from openapi_core.schema.servers.exceptions import InvalidServer
1515
from openapi_core.security.exceptions import SecurityError
16+
from openapi_core.templating.paths.exceptions import PathError
1617
from openapi_core.unmarshalling.schemas.enums import UnmarshalContext
1718
from openapi_core.unmarshalling.schemas.exceptions import (
1819
UnmarshalError, ValidateError,
@@ -30,7 +31,7 @@ def validate(self, request):
3031
try:
3132
path, operation, _, _, _ = self._find_path(request)
3233
# don't process if operation errors
33-
except (InvalidServer, InvalidPath, InvalidOperation) as exc:
34+
except PathError as exc:
3435
return RequestValidationResult([exc, ], None, None, None)
3536

3637
try:
@@ -53,7 +54,7 @@ def validate(self, request):
5354
def _validate_parameters(self, request):
5455
try:
5556
path, operation, _, _, _ = self._find_path(request)
56-
except (InvalidServer, InvalidPath, InvalidOperation) as exc:
57+
except PathError as exc:
5758
return RequestValidationResult([exc, ], None, None)
5859

5960
params, params_errors = self._get_parameters(
@@ -67,7 +68,7 @@ def _validate_parameters(self, request):
6768
def _validate_body(self, request):
6869
try:
6970
_, operation, _, _, _ = self._find_path(request)
70-
except (InvalidServer, InvalidOperation) as exc:
71+
except PathError as exc:
7172
return RequestValidationResult([exc, ], None, None)
7273

7374
body, body_errors = self._get_body(request, operation)

openapi_core/validation/response/validators.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
InvalidResponse, MissingResponseContent,
99
)
1010
from openapi_core.schema.servers.exceptions import InvalidServer
11+
from openapi_core.templating.paths.exceptions import PathError
1112
from openapi_core.unmarshalling.schemas.enums import UnmarshalContext
1213
from openapi_core.unmarshalling.schemas.exceptions import (
1314
UnmarshalError, ValidateError,
@@ -22,7 +23,7 @@ def validate(self, request, response):
2223
try:
2324
_, operation, _, _, _ = self._find_path(request)
2425
# don't process if operation errors
25-
except (InvalidServer, InvalidPath, InvalidOperation) as exc:
26+
except PathError as exc:
2627
return ResponseValidationResult([exc, ], None, None)
2728

2829
try:
@@ -47,7 +48,7 @@ def _validate_data(self, request, response):
4748
try:
4849
_, operation, _, _, _ = self._find_path(request)
4950
# don't process if operation errors
50-
except (InvalidServer, InvalidPath, InvalidOperation) as exc:
51+
except PathError as exc:
5152
return ResponseValidationResult([exc, ], None, None)
5253

5354
try:

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ lazy-object-proxy
44
strict_rfc3339
55
isodate
66
attrs
7+
parse==1.14.0

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ install_requires =
3030
isodate
3131
attrs
3232
werkzeug
33+
parse
3334
backports.functools-lru-cache; python_version<"3.0"
3435
backports.functools-partialmethod; python_version<"3.0"
3536
tests_require =

tests/integration/contrib/flask/test_flask_decorator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ def test_server_error(self, client):
8080
'errors': [
8181
{
8282
'class': (
83-
"<class 'openapi_core.schema.servers.exceptions."
84-
"InvalidServer'>"
83+
"<class 'openapi_core.templating.paths.exceptions."
84+
"PathNotFound'>"
8585
),
8686
'status': 500,
8787
'title': (
88-
'Invalid request server '
88+
'Path not found for '
8989
'https://localhost/browse/{id}/'
9090
),
9191
}

tests/integration/contrib/flask/test_flask_views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ def test_server_error(self, client):
6868
'errors': [
6969
{
7070
'class': (
71-
"<class 'openapi_core.schema.servers.exceptions."
72-
"InvalidServer'>"
71+
"<class 'openapi_core.templating.paths.exceptions."
72+
"PathNotFound'>"
7373
),
7474
'status': 500,
7575
'title': (
76-
'Invalid request server '
76+
'Path not found for '
7777
'https://localhost/browse/{id}/'
7878
),
7979
}

0 commit comments

Comments
 (0)