From 41c54b863c60e3d65647f9744b5949ccb5476837 Mon Sep 17 00:00:00 2001 From: Nino <52693877+lilylee1874@users.noreply.github.com> Date: Mon, 2 Oct 2023 10:59:04 +0800 Subject: [PATCH] fix: flask mimetype check (#349) * fix flask mimetype check Signed-off-by: Nino * format Signed-off-by: Nino --------- Signed-off-by: Nino --- pyproject.toml | 2 +- spectree/plugins/flask_plugin.py | 9 +++------ tests/flask_imports/dry_plugin_flask.py | 6 ++++++ 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9239fe29..aafa1088 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/spectree/plugins/flask_plugin.py b/spectree/plugins/flask_plugin.py index 59547652..9dee9053 100644 --- a/spectree/plugins/flask_plugin.py +++ b/spectree/plugins/flask_plugin.py @@ -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, diff --git a/tests/flask_imports/dry_plugin_flask.py b/tests/flask_imports/dry_plugin_flask.py index 2bb04d7f..d14697bc 100644 --- a/tests/flask_imports/dry_plugin_flask.py +++ b/tests/flask_imports/dry_plugin_flask.py @@ -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")