Skip to content

Commit cd45928

Browse files
AdamLeyshonsloria
andauthored
Fix serialisation problem with return codes when used with flask-restful (#168)
* Create the response object then set the response code on the response by unpacking the tuple or default to 200. Return a tuple consisting only of the response object allowing flask-restful to unpack and serialise the response properly. * Fix indentation * Update changelog Co-authored-by: Steven Loria <[email protected]>
1 parent 55255f5 commit cd45928

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

CHANGELOG.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Changelog
22
---------
33

4+
0.8.7 (unreleased)
5+
******************
6+
7+
Bug fixes:
8+
9+
* Fix serialisation problem with return codes when used with flask-restful (:issue:`98`, :issue:`93`).
10+
Thanks :user:`AdamLeyshon` for the PR.
11+
412
0.8.6 (2020-03-01)
513
******************
614

@@ -25,6 +33,7 @@ Bug fixes:
2533

2634
* Fix passing ``default_in`` argument when generating parameters (:issue:`165`).
2735
Thanks :user:`d42` for reporting and thanks :user:`zzz4zzz` for the fix.
36+
>>>>>>> master
2837

2938
0.8.3 (2019-09-17)
3039
******************

flask_apispec/wrapper.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# -*- coding: utf-8 -*-
2+
from flask import Response
3+
24
try:
35
from collections.abc import Mapping
46
except ImportError: # Python 2
57
from collections import Mapping
68

7-
89
import flask
910
import marshmallow as ma
1011
import werkzeug
@@ -13,17 +14,18 @@
1314

1415
from flask_apispec import utils
1516

16-
1717
MARSHMALLOW_VERSION_INFO = tuple(
1818
[int(part) for part in ma.__version__.split('.') if part.isdigit()]
1919
)
2020

21+
2122
class Wrapper(object):
2223
"""Apply annotations to a view function.
2324
2425
:param func: View function to wrap
2526
:param instance: Optional instance or parent
2627
"""
28+
2729
def __init__(self, func, instance=None):
2830
self.func = func
2931
self.instance = instance
@@ -49,7 +51,7 @@ def call_view(self, *args, **kwargs):
4951
elif isinstance(parsed, Mapping):
5052
kwargs.update(parsed)
5153
else:
52-
args += (parsed, )
54+
args += (parsed,)
5355

5456
return self.func(*args, **kwargs)
5557

@@ -65,14 +67,20 @@ def marshal_result(self, unpacked, status_code):
6567
output = dumped.data if MARSHMALLOW_VERSION_INFO[0] < 3 else dumped
6668
else:
6769
output = unpacked[0]
68-
return format_output((format_response(output), ) + unpacked[1:])
70+
71+
response_object = format_response(output) # type: Response
72+
response_object.status_code = unpacked[1] or 200
73+
return format_output((response_object,))
74+
6975

7076
def identity(value):
7177
return value
7278

79+
7380
def unpack(resp):
74-
resp = resp if isinstance(resp, tuple) else (resp, )
75-
return resp + (None, ) * (3 - len(resp))
81+
resp = resp if isinstance(resp, tuple) else (resp,)
82+
return resp + (None,) * (3 - len(resp))
83+
7684

7785
def format_output(values):
7886
while values[-1] is None:

0 commit comments

Comments
 (0)