Skip to content

Commit

Permalink
fix: flask mimetype check (#349)
Browse files Browse the repository at this point in the history
* fix flask mimetype check

Signed-off-by: Nino <lilylee1874@outlook.com>

* format

Signed-off-by: Nino <lilylee1874@outlook.com>

---------

Signed-off-by: Nino <lilylee1874@outlook.com>
  • Loading branch information
lilylee1874 authored Oct 2, 2023
1 parent eb6a554 commit 41c54b8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "spectree"
version = "1.2.4"
version = "1.2.5"
dynamic = []
description = "generate OpenAPI document and validate request&response with Python annotations."
readme = "README.md"
Expand Down
9 changes: 3 additions & 6 deletions spectree/plugins/flask_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,9 @@ def request_validation(self, request, query, json, form, headers, cookies):
req_headers = dict(iter(request.headers)) or {}
req_cookies = get_multidict_items(request.cookies) or {}
has_data = request.method not in ("GET", "DELETE")
use_json = json and has_data and request.mimetype == "application/json"
use_form = (
form
and has_data
and any([x in request.mimetype for x in self.FORM_MIMETYPE])
)
# flask Request.mimetype is already normalized
use_json = json and has_data and request.mimetype not in self.FORM_MIMETYPE
use_form = form and has_data and request.mimetype in self.FORM_MIMETYPE

request.context = Context(
query.parse_obj(req_query) if query else None,
Expand Down
6 changes: 6 additions & 0 deletions tests/flask_imports/dry_plugin_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ def test_flask_validate_post_data(client, fragment):
assert resp.status_code == 200, resp.json
assert resp.json["score"] == sorted(resp.json["score"], reverse=False)

# POST without body
resp = client.post(
f"/api/{fragment}/flask?order=0",
)
assert resp.status_code == 422, resp.content


def test_flask_no_response(client):
resp = client.get("/api/no_response")
Expand Down

0 comments on commit 41c54b8

Please sign in to comment.