Skip to content

Commit 229e594

Browse files
committed
Response finder
1 parent 086e0b1 commit 229e594

File tree

7 files changed

+51
-36
lines changed

7 files changed

+51
-36
lines changed

openapi_core/schema/operations/models.py

-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22
"""OpenAPI core operations models module"""
3-
from openapi_core.schema.responses.exceptions import InvalidResponse
43

54

65
class Operation(object):
@@ -29,18 +28,3 @@ def __init__(
2928

3029
def __getitem__(self, name):
3130
return self.parameters[name]
32-
33-
def get_response(self, http_status='default'):
34-
# @todo: move to Responses object
35-
try:
36-
return self.responses[http_status]
37-
except KeyError:
38-
# try range
39-
http_status_range = '{0}XX'.format(http_status[0])
40-
if http_status_range in self.responses:
41-
return self.responses[http_status_range]
42-
43-
if 'default' not in self.responses:
44-
raise InvalidResponse(http_status, self.responses)
45-
46-
return self.responses['default']

openapi_core/schema/responses/exceptions.py

-10
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@ class OpenAPIResponseError(OpenAPIMappingError):
77
pass
88

99

10-
@attr.s(hash=True)
11-
class InvalidResponse(OpenAPIResponseError):
12-
http_status = attr.ib()
13-
responses = attr.ib()
14-
15-
def __str__(self):
16-
return "Unknown response http status: {0}".format(
17-
str(self.http_status))
18-
19-
2010
@attr.s(hash=True)
2111
class MissingResponseContent(OpenAPIResponseError):
2212
response = attr.ib()

openapi_core/templating/responses/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import attr
2+
3+
from openapi_core.exceptions import OpenAPIError
4+
5+
6+
class ResponseFinderError(OpenAPIError):
7+
"""Response finder error"""
8+
9+
10+
@attr.s(hash=True)
11+
class ResponseNotFound(ResponseFinderError):
12+
"""Find response error"""
13+
http_status = attr.ib()
14+
responses = attr.ib()
15+
16+
def __str__(self):
17+
return "Unknown response http status: {0}".format(
18+
str(self.http_status))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from openapi_core.templating.responses.exceptions import ResponseNotFound
2+
3+
4+
class ResponseFinder(object):
5+
6+
def __init__(self, responses):
7+
self.responses = responses
8+
9+
def find(self, http_status='default'):
10+
try:
11+
return self.responses[http_status]
12+
except KeyError:
13+
pass
14+
15+
# try range
16+
http_status_range = '{0}XX'.format(http_status[0])
17+
if http_status_range in self.responses:
18+
return self.responses[http_status_range]
19+
20+
if 'default' not in self.responses:
21+
raise ResponseNotFound(http_status, self.responses)
22+
23+
return self.responses['default']

openapi_core/validation/response/validators.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
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.responses.exceptions import (
5-
InvalidResponse, MissingResponseContent,
6-
)
4+
from openapi_core.schema.responses.exceptions import MissingResponseContent
75
from openapi_core.templating.media_types.exceptions import MediaTypeFinderError
86
from openapi_core.templating.paths.exceptions import PathError
7+
from openapi_core.templating.responses.exceptions import ResponseFinderError
98
from openapi_core.unmarshalling.schemas.enums import UnmarshalContext
109
from openapi_core.unmarshalling.schemas.exceptions import (
1110
UnmarshalError, ValidateError,
@@ -27,7 +26,7 @@ def validate(self, request, response):
2726
operation_response = self._get_operation_response(
2827
operation, response)
2928
# don't process if operation errors
30-
except InvalidResponse as exc:
29+
except ResponseFinderError as exc:
3130
return ResponseValidationResult(errors=[exc, ])
3231

3332
data, data_errors = self._get_data(response, operation_response)
@@ -43,7 +42,9 @@ def validate(self, request, response):
4342
)
4443

4544
def _get_operation_response(self, operation, response):
46-
return operation.get_response(str(response.status_code))
45+
from openapi_core.templating.responses.finders import ResponseFinder
46+
finder = ResponseFinder(operation.responses)
47+
return finder.find(str(response.status_code))
4748

4849
def _validate_data(self, request, response):
4950
try:
@@ -56,7 +57,7 @@ def _validate_data(self, request, response):
5657
operation_response = self._get_operation_response(
5758
operation, response)
5859
# don't process if operation errors
59-
except InvalidResponse as exc:
60+
except ResponseFinderError as exc:
6061
return ResponseValidationResult(errors=[exc, ])
6162

6263
data, data_errors = self._get_data(response, operation_response)

tests/integration/validation/test_validators.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
from openapi_core.extensions.models.models import BaseModel
99
from openapi_core.schema.parameters.exceptions import MissingRequiredParameter
1010
from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
11-
from openapi_core.schema.responses.exceptions import (
12-
MissingResponseContent, InvalidResponse,
13-
)
11+
from openapi_core.schema.responses.exceptions import MissingResponseContent
1412
from openapi_core.shortcuts import create_spec
1513
from openapi_core.templating.media_types.exceptions import MediaTypeNotFound
1614
from openapi_core.templating.paths.exceptions import (
1715
PathNotFound, OperationNotFound,
1816
)
17+
from openapi_core.templating.responses.exceptions import ResponseNotFound
1918
from openapi_core.testing import MockRequest, MockResponse
2019
from openapi_core.unmarshalling.schemas.exceptions import InvalidSchemaValue
2120
from openapi_core.validation.exceptions import InvalidSecurity
@@ -450,7 +449,7 @@ def test_invalid_response(self, validator):
450449
result = validator.validate(request, response)
451450

452451
assert len(result.errors) == 1
453-
assert type(result.errors[0]) == InvalidResponse
452+
assert type(result.errors[0]) == ResponseNotFound
454453
assert result.data is None
455454
assert result.headers == {}
456455

0 commit comments

Comments
 (0)