jmcarp / flask-apispec

MIT License
655 stars 156 forks source link

Return Response Object from View Functions #184

Closed c-kruse closed 4 years ago

c-kruse commented 4 years ago

Fixes 🤞

"Version 0.8.7 regression" https://github.com/jmcarp/flask-apispec/issues/181 @decaz

Caused by

"Fix serialization problem with return codes when used with flask-restful" https://github.com/jmcarp/flask-apispec/pull/168 @AdamLeyshon

What?

Flask gives developers a lot of leeway when it comes view function return values (flask docs.) Flask-Restful did not play well with the particular structure pre 0.8.7, flask-apispec's decorators were using.

Pre 0.8.7 flask-apispec's decorators return a tuple with the first value being a Response object, and optionally a second and third value for the status_code and headers. See point in time here.. In https://github.com/jmcarp/flask-apispec/pull/168 we attempted to fix this by returning a tuple containing only a response object (neglecting to update the headers.)

In this PR, I try and unpack the view function's return value in a safer way (acknowledging the possibility of someone wanting to return a tuple with (response, headers)). Then letting marshal_result just worry about marshaling, and using flask's make_response to return a Response object with updated status code and headers.