Skip to content

Commit 3526fa0

Browse files
committed
Media type finder
1 parent 1747433 commit 3526fa0

File tree

17 files changed

+78
-72
lines changed

17 files changed

+78
-72
lines changed

openapi_core/contrib/falcon/handlers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from falcon.status_codes import (
66
HTTP_400, HTTP_404, HTTP_405, HTTP_415,
77
)
8-
from openapi_core.schema.media_types.exceptions import InvalidContentType
8+
from openapi_core.templating.media_types.exceptions import MediaTypeNotFound
99
from openapi_core.templating.paths.exceptions import (
1010
ServerNotFound, OperationNotFound, PathNotFound,
1111
)
@@ -17,7 +17,7 @@ class FalconOpenAPIErrorsHandler(object):
1717
ServerNotFound: 400,
1818
OperationNotFound: 405,
1919
PathNotFound: 404,
20-
InvalidContentType: 415,
20+
MediaTypeNotFound: 415,
2121
}
2222

2323
FALCON_STATUS_CODES = {

openapi_core/contrib/flask/handlers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from flask.globals import current_app
33
from flask.json import dumps
44

5-
from openapi_core.schema.media_types.exceptions import InvalidContentType
5+
from openapi_core.templating.media_types.exceptions import MediaTypeNotFound
66
from openapi_core.templating.paths.exceptions import (
77
ServerNotFound, OperationNotFound, PathNotFound,
88
)
@@ -14,7 +14,7 @@ class FlaskOpenAPIErrorsHandler(object):
1414
ServerNotFound: 400,
1515
OperationNotFound: 405,
1616
PathNotFound: 404,
17-
InvalidContentType: 415,
17+
MediaTypeNotFound: 415,
1818
}
1919

2020
@classmethod

openapi_core/schema/content/exceptions.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,3 @@
55

66
class OpenAPIContentError(OpenAPIMappingError):
77
pass
8-
9-
10-
@attr.s(hash=True)
11-
class MimeTypeNotFound(OpenAPIContentError):
12-
mimetype = attr.ib()
13-
availableMimetypes = attr.ib()
14-
15-
def __str__(self):
16-
return "Mimetype not found: {0}. Valid mimetypes: {1}".format(
17-
self.mimetype, self.availableMimetypes)

openapi_core/schema/content/models.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
"""OpenAPI core content models module"""
2-
import fnmatch
3-
4-
from six import iteritems
5-
6-
from openapi_core.schema.content.exceptions import MimeTypeNotFound
72

83

94
class Content(dict):
10-
11-
def __getitem__(self, mimetype):
12-
try:
13-
return super(Content, self).__getitem__(mimetype)
14-
except KeyError:
15-
pass
16-
17-
for key, value in iteritems(self):
18-
if fnmatch.fnmatch(mimetype, key):
19-
return value
20-
21-
raise MimeTypeNotFound(mimetype, list(self.keys()))
5+
pass
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
"""OpenAPI core request bodies models module"""
2-
from openapi_core.schema.content.exceptions import MimeTypeNotFound
3-
from openapi_core.schema.media_types.exceptions import InvalidContentType
42

53

64
class RequestBody(object):
@@ -11,9 +9,3 @@ def __init__(self, content, required=False, extensions=None):
119
self.required = required
1210

1311
self.extensions = extensions and dict(extensions) or {}
14-
15-
def __getitem__(self, mimetype):
16-
try:
17-
return self.content[mimetype]
18-
except MimeTypeNotFound:
19-
raise InvalidContentType(mimetype)
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
"""OpenAPI core responses models module"""
2-
from openapi_core.schema.content.exceptions import MimeTypeNotFound
3-
from openapi_core.schema.media_types.exceptions import InvalidContentType
42

53

64
class Response(object):
@@ -15,12 +13,3 @@ def __init__(
1513
self.links = links and dict(links) or {}
1614

1715
self.extensions = extensions and dict(extensions) or {}
18-
19-
def __getitem__(self, mimetype):
20-
return self.get_content_type(mimetype)
21-
22-
def get_content_type(self, mimetype):
23-
try:
24-
return self.content[mimetype]
25-
except MimeTypeNotFound:
26-
raise InvalidContentType(mimetype)

openapi_core/templating/media_types/__init__.py

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import attr
2+
3+
from openapi_core.exceptions import OpenAPIError
4+
5+
6+
class MediaTypeFinderError(OpenAPIError):
7+
"""Media type finder error"""
8+
9+
10+
@attr.s(hash=True)
11+
class MediaTypeNotFound(MediaTypeFinderError):
12+
mimetype = attr.ib()
13+
availableMimetypes = attr.ib()
14+
15+
def __str__(self):
16+
return (
17+
"Content for the following mimetype not found: {0}. "
18+
"Valid mimetypes: {1}"
19+
).format(self.mimetype, self.availableMimetypes)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""OpenAPI core templating media types finders module"""
2+
import fnmatch
3+
4+
from six import iteritems
5+
6+
from openapi_core.templating.media_types.exceptions import MediaTypeNotFound
7+
8+
9+
class MediaTypeFinder(object):
10+
11+
def __init__(self, content):
12+
self.content = content
13+
14+
def find(self, request):
15+
try:
16+
return self.content[request.mimetype]
17+
except KeyError:
18+
pass
19+
20+
for key, value in iteritems(self.content):
21+
if fnmatch.fnmatch(request.mimetype, key):
22+
return value
23+
24+
raise MediaTypeNotFound(request.mimetype, list(self.content.keys()))

openapi_core/validation/request/validators.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
from openapi_core.casting.schemas.exceptions import CastError
66
from openapi_core.deserializing.exceptions import DeserializeError
7-
from openapi_core.schema.media_types.exceptions import InvalidContentType
87
from openapi_core.schema.parameters.exceptions import (
98
MissingRequiredParameter, MissingParameter,
109
)
1110
from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
1211
from openapi_core.security.exceptions import SecurityError
12+
from openapi_core.templating.media_types.exceptions import MediaTypeFinderError
1313
from openapi_core.templating.paths.exceptions import PathError
1414
from openapi_core.unmarshalling.schemas.enums import UnmarshalContext
1515
from openapi_core.unmarshalling.schemas.exceptions import (
@@ -154,8 +154,9 @@ def _get_body(self, request, operation):
154154
return None, []
155155

156156
try:
157-
media_type = operation.request_body[request.mimetype]
158-
except InvalidContentType as exc:
157+
media_type = self._get_media_type(
158+
operation.request_body.content, request)
159+
except MediaTypeFinderError as exc:
159160
return None, [exc, ]
160161

161162
try:

0 commit comments

Comments
 (0)