Skip to content

Commit 1d99bb9

Browse files
committed
Handle forms exception (consumes) on class too
1 parent c6e1c65 commit 1d99bb9

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

flask_restplus/swagger.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,10 @@ def serialize_operation(self, doc, method):
363363
if doc.get('deprecated') or doc[method].get('deprecated'):
364364
operation['deprecated'] = True
365365
# Handle form exceptions:
366-
if operation['parameters'] and any(p['in'] == 'formData' for p in operation['parameters']):
367-
if any(p['type'] == 'file' for p in operation['parameters']):
366+
doc_params = list(doc.get('params', {}).values())
367+
all_params = doc_params + (operation['parameters'] or [])
368+
if all_params and any(p['in'] == 'formData' for p in all_params):
369+
if any(p['type'] == 'file' for p in all_params):
368370
operation['consumes'] = ['multipart/form-data']
369371
else:
370372
operation['consumes'] = ['application/x-www-form-urlencoded', 'multipart/form-data']

tests/test_swagger.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,33 @@ def get(self):
710710

711711
assert op['consumes'] == ['multipart/form-data']
712712

713+
def test_parser_parameter_in_files_on_class(self, api, client):
714+
parser = api.parser()
715+
parser.add_argument('in_files', type=FileStorage, location='files')
716+
717+
@api.route('/with-parser/', endpoint='with-parser')
718+
@api.expect(parser)
719+
class WithParserResource(restplus.Resource):
720+
def get(self):
721+
return {}
722+
723+
data = client.get_specs()
724+
assert '/with-parser/' in data['paths']
725+
726+
path = data['paths']['/with-parser/']
727+
assert len(path['parameters']) == 1
728+
729+
parameter = path['parameters'][0]
730+
assert parameter['name'] == 'in_files'
731+
assert parameter['type'] == 'file'
732+
assert parameter['in'] == 'formData'
733+
734+
assert 'consumes' not in path
735+
736+
op = path['get']
737+
assert 'consumes' in op
738+
assert op['consumes'] == ['multipart/form-data']
739+
713740
def test_explicit_parameters(self, api, client):
714741
@api.route('/name/<int:age>/', endpoint='by-name')
715742
class ByNameResource(restplus.Resource):

0 commit comments

Comments
 (0)