Skip to content

media type finder find sig refactor #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions openapi_core/templating/media_types/finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class MediaTypeFinder:
def __init__(self, content):
self.content = content

def find(self, request):
if request.mimetype in self.content:
return self.content / request.mimetype, request.mimetype
def find(self, mimetype):
if mimetype in self.content:
return self.content / mimetype, mimetype

if request.mimetype:
if mimetype:
for key, value in self.content.items():
if fnmatch.fnmatch(request.mimetype, key):
if fnmatch.fnmatch(mimetype, key):
return value, key

raise MediaTypeNotFound(request.mimetype, list(self.content.keys()))
raise MediaTypeNotFound(mimetype, list(self.content.keys()))
2 changes: 1 addition & 1 deletion openapi_core/validation/request/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def _get_body(self, request, operation):

try:
media_type, mimetype = self._get_media_type(
request_body / "content", request
request_body / "content", request.mimetype
)
except MediaTypeFinderError as exc:
return None, [exc]
Expand Down
2 changes: 1 addition & 1 deletion openapi_core/validation/response/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _get_data(self, response, operation_response):

try:
media_type, mimetype = self._get_media_type(
operation_response / "content", response
operation_response / "content", response.mimetype
)
except MediaTypeFinderError as exc:
return None, [exc]
Expand Down
4 changes: 2 additions & 2 deletions openapi_core/validation/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def schema_unmarshallers_factory(self):
def _find_path(self, request):
return self.path_finder.find(request)

def _get_media_type(self, content, request_or_response):
def _get_media_type(self, content, mimetype):
from openapi_core.templating.media_types.finders import MediaTypeFinder

finder = MediaTypeFinder(content)
return finder.find(request_or_response)
return finder.find(mimetype)

def _deserialise_data(self, mimetype, value):
deserializer = self.media_type_deserializers_factory.create(mimetype)
Expand Down
16 changes: 8 additions & 8 deletions tests/unit/templating/test_media_types_finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ def finder(self, content):
return MediaTypeFinder(content)

def test_exact(self, finder, content):
response = MockResponse("", mimetype="application/json")
mimetype = "application/json"

_, mimetype = finder.find(response)
_, mimetype = finder.find(mimetype)
assert mimetype == "application/json"

def test_match(self, finder, content):
response = MockResponse("", mimetype="text/html")
mimetype = "text/html"

_, mimetype = finder.find(response)
_, mimetype = finder.find(mimetype)
assert mimetype == "text/*"

def test_not_found(self, finder, content):
response = MockResponse("", mimetype="unknown")
mimetype = "unknown"

with pytest.raises(MediaTypeNotFound):
finder.find(response)
finder.find(mimetype)

def test_missing(self, finder, content):
response = MockResponse("", mimetype=None)
mimetype = None

with pytest.raises(MediaTypeNotFound):
finder.find(response)
finder.find(mimetype)