Skip to content

Commit 0bf0016

Browse files
committed
Invalid operation error
1 parent a86c49d commit 0bf0016

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

openapi_core/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class InvalidContentTypeError(OpenAPIMappingError):
2121
pass
2222

2323

24+
class InvalidOperationError(OpenAPIMappingError):
25+
pass
26+
27+
2428
class InvalidServerError(OpenAPIMappingError):
2529
pass
2630

openapi_core/specs.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from openapi_spec_validator import openapi_v3_spec_validator
77

88
from openapi_core.components import ComponentsFactory
9+
from openapi_core.exceptions import InvalidOperationError
910
from openapi_core.infos import InfoFactory
1011
from openapi_core.paths import PathsGenerator
1112
from openapi_core.schemas import SchemaRegistry
@@ -35,7 +36,12 @@ def get_server_url(self, index=0):
3536
return self.servers[index].default_url
3637

3738
def get_operation(self, path_pattern, http_method):
38-
return self.paths[path_pattern].operations[http_method]
39+
try:
40+
return self.paths[path_pattern].operations[http_method]
41+
except KeyError:
42+
raise InvalidOperationError(
43+
"Unknown operation path {0} with method {1}".format(
44+
path_pattern, http_method))
3945

4046
def get_schema(self, name):
4147
return self.components.schemas[name]

openapi_core/wrappers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ def get_operation(self, request, spec):
4141

4242
operation_pattern = request.full_url_pattern.replace(
4343
server.default_url, '')
44+
method = request.method.lower()
4445

45-
return spec.get_operation(operation_pattern, request.method.lower())
46+
return spec.get_operation(operation_pattern, method)
4647

4748
def _get_server(self, request, spec):
4849
for server in spec.servers:

tests/unit/test_specs.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,50 @@
11
import mock
22
import pytest
33

4+
from openapi_core.exceptions import InvalidOperationError
5+
from openapi_core.paths import Path
46
from openapi_core.specs import Spec
57

68

79
class TestSpecs(object):
810

911
@pytest.fixture
10-
def spec(self):
12+
def path1(self):
13+
operations = {
14+
'get': mock.sentinel.path1_get,
15+
}
16+
return Path('path1', operations)
17+
18+
@pytest.fixture
19+
def path2(self):
20+
operations = {
21+
'post': mock.sentinel.path2_psot,
22+
}
23+
return Path('path2', operations)
24+
25+
@pytest.fixture
26+
def spec(self, path1, path2):
1127
servers = []
1228
paths = {
13-
'get': mock.sentinel.get,
14-
'post': mock.sentinel.post,
29+
'/path1': path1,
30+
'/path2': path2,
1531
}
1632
return Spec(servers, paths)
1733

18-
@property
1934
def test_iteritems(self, spec):
2035
for path_name in spec.paths.keys():
2136
assert spec[path_name] ==\
2237
spec.paths[path_name]
38+
39+
def test_valid(self, spec):
40+
operation = spec.get_operation('/path1', 'get')
41+
42+
assert operation == mock.sentinel.path1_get
43+
44+
def test_invalid_path(self, spec):
45+
with pytest.raises(InvalidOperationError):
46+
spec.get_operation('/path3', 'get')
47+
48+
def test_invalid_method(self, spec):
49+
with pytest.raises(InvalidOperationError):
50+
spec.get_operation('/path1', 'post')

0 commit comments

Comments
 (0)