Skip to content

Commit eadde7e

Browse files
committed
Finder exception
1 parent 00e8b08 commit eadde7e

File tree

13 files changed

+111
-45
lines changed

13 files changed

+111
-45
lines changed

openapi_core/contrib/flask/handlers.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
from flask.json import dumps
44

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

810

911
class FlaskOpenAPIErrorsHandler(object):
1012

1113
OPENAPI_ERROR_STATUS = {
12-
InvalidServer: 500,
14+
OperationNotFound: 500,
15+
PathNotFound: 500,
1316
InvalidContentType: 415,
1417
}
1518

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

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
"""OpenAPI core templating paths finders module"""
2+
from openapi_core.schema.operations.exceptions import InvalidOperation
3+
from openapi_core.schema.paths.exceptions import InvalidPath
4+
from openapi_core.schema.servers.exceptions import InvalidServer
5+
from openapi_core.templating.paths.exceptions import (
6+
PathNotFound, OperationNotFound,
7+
)
28
from openapi_core.templating.paths.util import get_operation_pattern
39

410

@@ -8,16 +14,21 @@ def __init__(self, spec):
814
self.spec = spec
915

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

2233
def _get_operation_pattern(self, request):
2334
server = self.spec.get_server(request.full_url_pattern)
+26
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

+4-6
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
from openapi_core.casting.schemas.exceptions import CastError
66
from openapi_core.deserializing.exceptions import DeserializeError
77
from openapi_core.schema.media_types.exceptions import InvalidContentType
8-
from openapi_core.schema.operations.exceptions import InvalidOperation
98
from openapi_core.schema.parameters.exceptions import (
109
MissingRequiredParameter, MissingParameter,
1110
)
12-
from openapi_core.schema.paths.exceptions import InvalidPath
1311
from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
14-
from openapi_core.schema.servers.exceptions import InvalidServer
1512
from openapi_core.security.exceptions import SecurityError
13+
from openapi_core.templating.paths.exceptions import PathError
1614
from openapi_core.unmarshalling.schemas.enums import UnmarshalContext
1715
from openapi_core.unmarshalling.schemas.exceptions import (
1816
UnmarshalError, ValidateError,
@@ -30,7 +28,7 @@ def validate(self, request):
3028
try:
3129
path, operation, _, _, _ = self._find_path(request)
3230
# don't process if operation errors
33-
except (InvalidServer, InvalidPath, InvalidOperation) as exc:
31+
except PathError as exc:
3432
return RequestValidationResult([exc, ], None, None, None)
3533

3634
try:
@@ -53,7 +51,7 @@ def validate(self, request):
5351
def _validate_parameters(self, request):
5452
try:
5553
path, operation, _, _, _ = self._find_path(request)
56-
except (InvalidServer, InvalidPath, InvalidOperation) as exc:
54+
except PathError as exc:
5755
return RequestValidationResult([exc, ], None, None)
5856

5957
params, params_errors = self._get_parameters(
@@ -67,7 +65,7 @@ def _validate_parameters(self, request):
6765
def _validate_body(self, request):
6866
try:
6967
_, operation, _, _, _ = self._find_path(request)
70-
except (InvalidServer, InvalidOperation) as exc:
68+
except PathError as exc:
7169
return RequestValidationResult([exc, ], None, None)
7270

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

openapi_core/validation/response/validators.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
"""OpenAPI core validation response validators module"""
22
from openapi_core.casting.schemas.exceptions import CastError
33
from openapi_core.deserializing.exceptions import DeserializeError
4-
from openapi_core.schema.operations.exceptions import InvalidOperation
54
from openapi_core.schema.media_types.exceptions import InvalidContentType
6-
from openapi_core.schema.paths.exceptions import InvalidPath
75
from openapi_core.schema.responses.exceptions import (
86
InvalidResponse, MissingResponseContent,
97
)
10-
from openapi_core.schema.servers.exceptions import InvalidServer
8+
from openapi_core.templating.paths.exceptions import PathError
119
from openapi_core.unmarshalling.schemas.enums import UnmarshalContext
1210
from openapi_core.unmarshalling.schemas.exceptions import (
1311
UnmarshalError, ValidateError,
@@ -22,7 +20,7 @@ def validate(self, request, response):
2220
try:
2321
_, operation, _, _, _ = self._find_path(request)
2422
# don't process if operation errors
25-
except (InvalidServer, InvalidPath, InvalidOperation) as exc:
23+
except PathError as exc:
2624
return ResponseValidationResult([exc, ], None, None)
2725

2826
try:
@@ -47,7 +45,7 @@ def _validate_data(self, request, response):
4745
try:
4846
_, operation, _, _, _ = self._find_path(request)
4947
# don't process if operation errors
50-
except (InvalidServer, InvalidPath, InvalidOperation) as exc:
48+
except PathError as exc:
5149
return ResponseValidationResult([exc, ], None, None)
5250

5351
try:

requirements.txt

+1
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

+1
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

+3-3
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

+3-3
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
}

tests/integration/validation/test_minimal.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import pytest
22

3-
from openapi_core.schema.operations.exceptions import InvalidOperation
4-
from openapi_core.schema.paths.exceptions import InvalidPath
53
from openapi_core.shortcuts import create_spec
4+
from openapi_core.templating.paths.exceptions import (
5+
PathNotFound, OperationNotFound,
6+
)
67
from openapi_core.testing import MockRequest
78
from openapi_core.validation.request.validators import RequestValidator
89

@@ -45,7 +46,7 @@ def test_invalid_operation(self, factory, server, spec_path):
4546
result = validator.validate(request)
4647

4748
assert len(result.errors) == 1
48-
assert isinstance(result.errors[0], InvalidOperation)
49+
assert isinstance(result.errors[0], OperationNotFound)
4950
assert result.body is None
5051
assert result.parameters is None
5152

@@ -60,6 +61,6 @@ def test_invalid_path(self, factory, server, spec_path):
6061
result = validator.validate(request)
6162

6263
assert len(result.errors) == 1
63-
assert isinstance(result.errors[0], InvalidPath)
64+
assert isinstance(result.errors[0], PathNotFound)
6465
assert result.body is None
6566
assert result.parameters is None

tests/integration/validation/test_petstore.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
MissingRequiredParameter,
1717
)
1818
from openapi_core.schema.schemas.enums import SchemaType
19-
from openapi_core.schema.servers.exceptions import InvalidServer
2019
from openapi_core.shortcuts import (
2120
create_spec, validate_parameters, validate_body,
2221
)
22+
from openapi_core.templating.paths.exceptions import PathNotFound
2323
from openapi_core.testing import MockRequest, MockResponse
2424
from openapi_core.unmarshalling.schemas.exceptions import InvalidSchemaValue
2525
from openapi_core.validation.request.datatypes import RequestParameters
@@ -748,10 +748,10 @@ def test_post_pets_raises_invalid_server_error(self, spec):
748748
headers=headers, cookies=cookies,
749749
)
750750

751-
with pytest.raises(InvalidServer):
751+
with pytest.raises(PathNotFound):
752752
validate_parameters(spec, request)
753753

754-
with pytest.raises(InvalidServer):
754+
with pytest.raises(PathNotFound):
755755
validate_body(spec, request)
756756

757757
def test_get_pet(self, spec, response_validator):

tests/integration/validation/test_validators.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
InvalidContentType,
1010
)
1111
from openapi_core.extensions.models.models import BaseModel
12-
from openapi_core.schema.operations.exceptions import InvalidOperation
1312
from openapi_core.schema.parameters.exceptions import MissingRequiredParameter
14-
from openapi_core.schema.paths.exceptions import InvalidPath
1513
from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
1614
from openapi_core.schema.responses.exceptions import (
1715
MissingResponseContent, InvalidResponse,
1816
)
19-
from openapi_core.schema.servers.exceptions import InvalidServer
2017
from openapi_core.shortcuts import create_spec
18+
from openapi_core.templating.paths.exceptions import (
19+
PathNotFound, OperationNotFound,
20+
)
2121
from openapi_core.testing import MockRequest, MockResponse
2222
from openapi_core.unmarshalling.schemas.exceptions import InvalidSchemaValue
2323
from openapi_core.validation.exceptions import InvalidSecurity
@@ -56,7 +56,7 @@ def test_request_server_error(self, validator):
5656
result = validator.validate(request)
5757

5858
assert len(result.errors) == 1
59-
assert type(result.errors[0]) == InvalidServer
59+
assert type(result.errors[0]) == PathNotFound
6060
assert result.body is None
6161
assert result.parameters is None
6262

@@ -66,7 +66,7 @@ def test_invalid_path(self, validator):
6666
result = validator.validate(request)
6767

6868
assert len(result.errors) == 1
69-
assert type(result.errors[0]) == InvalidPath
69+
assert type(result.errors[0]) == PathNotFound
7070
assert result.body is None
7171
assert result.parameters is None
7272

@@ -76,7 +76,7 @@ def test_invalid_operation(self, validator):
7676
result = validator.validate(request)
7777

7878
assert len(result.errors) == 1
79-
assert type(result.errors[0]) == InvalidOperation
79+
assert type(result.errors[0]) == OperationNotFound
8080
assert result.body is None
8181
assert result.parameters is None
8282

@@ -428,18 +428,18 @@ def test_invalid_server(self, validator):
428428
result = validator.validate(request, response)
429429

430430
assert len(result.errors) == 1
431-
assert type(result.errors[0]) == InvalidServer
431+
assert type(result.errors[0]) == PathNotFound
432432
assert result.data is None
433433
assert result.headers is None
434434

435435
def test_invalid_operation(self, validator):
436-
request = MockRequest(self.host_url, 'get', '/v1')
436+
request = MockRequest(self.host_url, 'patch', '/v1/pets')
437437
response = MockResponse('Not Found', status_code=404)
438438

439439
result = validator.validate(request, response)
440440

441441
assert len(result.errors) == 1
442-
assert type(result.errors[0]) == InvalidPath
442+
assert type(result.errors[0]) == OperationNotFound
443443
assert result.data is None
444444
assert result.headers is None
445445

0 commit comments

Comments
 (0)