Skip to content

Commit

Permalink
Fixing issue python-restx#177, regarding file uploads as a list. This…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
TraivsBrookes96 committed May 11, 2023
1 parent ad2210c commit 7e33210
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions flask_restx/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
43 changes: 43 additions & 0 deletions tests/test_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int:age>/", endpoint="by-name")
class ByNameResource(restx.Resource):
Expand Down

0 comments on commit 7e33210

Please sign in to comment.