From 7e332108d928b51db7fbefa3fd9a0796659ec0e9 Mon Sep 17 00:00:00 2001 From: travisbrookes Date: Thu, 11 May 2023 18:57:34 +0100 Subject: [PATCH 1/4] Fixing issue #177, regarding file uploads as a list. This accounts for multiple arguments as part of the form and sets the correct "consumes" values so that "multipart/form-data" gets submitted, as with a single file, when a list of files is present. --- flask_restx/swagger.py | 3 +++ tests/test_swagger.py | 43 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/flask_restx/swagger.py b/flask_restx/swagger.py index ec0a1975..9f2fff72 100644 --- a/flask_restx/swagger.py +++ b/flask_restx/swagger.py @@ -504,6 +504,9 @@ def serialize_operation(self, doc, method): if all_params and any(p["in"] == "formData" for p in all_params): if any(p["type"] == "file" for p in all_params): operation["consumes"] = ["multipart/form-data"] + elif any(p["type"] == "array" and p["collectionFormat"] == "multi" for p in all_params + if "collectionFormat" in p): + operation["consumes"] = ["multipart/form-data"] else: operation["consumes"] = [ "application/x-www-form-urlencoded", diff --git a/tests/test_swagger.py b/tests/test_swagger.py index 8f181505..b15f82d1 100644 --- a/tests/test_swagger.py +++ b/tests/test_swagger.py @@ -791,6 +791,49 @@ def get(self): assert "consumes" in op assert op["consumes"] == ["multipart/form-data"] + def test_parser_parameter_in_files_append_only(self, api, client): + parser = api.parser() + parser.add_argument( + "in_files", type=FileStorage, location="files", action="append" + ) + + @api.route("/with-parser/", endpoint="with-parser") + class WithParserResource(restx.Resource): + @api.expect(parser) + def post(self): + return {} + + data = client.get_specs() + assert "/with-parser/" in data["paths"] + + path = data["paths"]["/with-parser/"] + op = path["post"] + assert op["consumes"][0] == "multipart/form-data" + + def test_parser_parameter_in_files_append_multiple_parameters(self, api, client): + parser = api.parser() + parser.add_argument( + "in_files", type=FileStorage, location="files", action="append" + ) + # Ensure that we have a second argument not of type file, and therefore will not have the collectionFormat + # attribute + parser.add_argument( + "arbitrary_arguments", type=str, location="form" + ) + + @api.route("/with-parser/", endpoint="with-parser") + class WithParserResource(restx.Resource): + @api.expect(parser) + def post(self): + return {} + + data = client.get_specs() + assert "/with-parser/" in data["paths"] + + path = data["paths"]["/with-parser/"] + op = path["post"] + assert op["consumes"][0] == "multipart/form-data" + def test_explicit_parameters(self, api, client): @api.route("/name//", endpoint="by-name") class ByNameResource(restx.Resource): From 9a8a822d7b008cfdb94362587c204338928a0403 Mon Sep 17 00:00:00 2001 From: Travis Brookes Date: Thu, 11 May 2023 19:02:41 +0100 Subject: [PATCH 2/4] Update CHANGELOG.rst Mentioning fix of bug #177 --- CHANGELOG.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a314e62d..6102a6da 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -25,6 +25,13 @@ Releases prior to 0.3.0 were “best effort” filled out, but are missing some info. If you see your contribution missing info, please open a PR on the Changelog! +.. _section-1.2.0: +1.2.0 +----- +.._bug_fixes-1.2.0 +Bug Fixes +Fixing issue #177 to allow file list uploads, whilst other arugments are present. + .. _section-1.1.0: 1.1.0 ----- From b81c5bbbc0c38c59e593bc6b5e6e394b23215691 Mon Sep 17 00:00:00 2001 From: Travis Brookes Date: Thu, 11 May 2023 19:02:57 +0100 Subject: [PATCH 3/4] Update CHANGELOG.rst --- CHANGELOG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 6102a6da..327d187e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -28,7 +28,7 @@ on the Changelog! .. _section-1.2.0: 1.2.0 ----- -.._bug_fixes-1.2.0 +.. _bug_fixes-1.2.0 Bug Fixes Fixing issue #177 to allow file list uploads, whilst other arugments are present. From 52a9fefae59d462cb32e7a79d584b398764086ce Mon Sep 17 00:00:00 2001 From: Travis Brookes Date: Thu, 11 May 2023 19:04:03 +0100 Subject: [PATCH 4/4] Update CHANGELOG.rst Ensuring formatting matches that of previous updates to this document --- CHANGELOG.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 327d187e..e33144c8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -30,7 +30,11 @@ on the Changelog! ----- .. _bug_fixes-1.2.0 Bug Fixes -Fixing issue #177 to allow file list uploads, whilst other arugments are present. +~~~~~~~~~ + +:: + + * Fixing issue #177 to allow file list uploads, whilst other arugments are present. .. _section-1.1.0: 1.1.0