Skip to content

Commit

Permalink
Fix returning flask Response objects in a tuple (#425)
Browse files Browse the repository at this point in the history
* Fix returning Response objects in tuple with status code and/or headers

* Use flasks code for dealing with tuples instead of my own

* Unit tests for returning flask reponse in tuple

* fix test, should be a dict, not a set

* Properly sort imports
  • Loading branch information
vimalloc authored and hjacobs committed Apr 4, 2017
1 parent 53908d3 commit 6f567ee
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions connexion/apis/flask_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ def _get_flask_response(cls, response, mimetype):
if flask_utils.is_flask_response(response):
return response

elif isinstance(response, tuple) and flask_utils.is_flask_response(response[0]):
return flask.current_app.make_response(response)

elif isinstance(response, tuple) and len(response) == 3:
data, status_code, headers = response
return cls._build_flask_response(mimetype, None,
Expand Down
10 changes: 10 additions & 0 deletions tests/api/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ def test_produce_decorator(simple_app):
assert get_bye.content_type == 'text/plain; charset=utf-8'


def test_returning_flask_response_tuple(simple_app):
app_client = simple_app.app.test_client()

result = app_client.get('/v1.0/flask_response_tuple') # type: flask.Response
assert result.status_code == 201
assert result.content_type == 'application/json'
result_data = json.loads(result.data.decode('utf-8', 'replace'))
assert result_data == {'foo': 'bar'}


def test_jsonifier(simple_app):
app_client = simple_app.app.test_client()

Expand Down
6 changes: 5 additions & 1 deletion tests/fakeapi/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from connexion import NoContent, ProblemException, problem
from connexion.apis import FlaskApi
from flask import redirect, request
from flask import jsonify, redirect, request


class DummyClass(object):
Expand Down Expand Up @@ -64,6 +64,10 @@ def get_bye(name):
return 'Goodbye {name}'.format(name=name)


def get_flask_response_tuple():
return jsonify({'foo': 'bar'}), 201


def get_bye_secure(name, user, token_info):
return 'Goodbye {name} (Secure: {user})'.format(name=name, user=user)

Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/simple/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ paths:
required: true
type: string

/flask_response_tuple:
get:
summary: Return flask response tuple
description: Test returning a flask response tuple
operationId: fakeapi.hello.get_flask_response_tuple
produces:
- application/json
responses:
200:
description: json response
schema:
type: object

/list/{name}:
get:
summary: Generate a greeting in a list
Expand Down

0 comments on commit 6f567ee

Please sign in to comment.