forked from orbit-oss/flask
Support returning list as JSON
This commit is contained in:
parent
1626aff602
commit
ca2bfbb0ac
3 changed files with 16 additions and 4 deletions
|
|
@ -1830,6 +1830,9 @@ class Flask(Scaffold):
|
||||||
``dict``
|
``dict``
|
||||||
A dictionary that will be jsonify'd before being returned.
|
A dictionary that will be jsonify'd before being returned.
|
||||||
|
|
||||||
|
``list``
|
||||||
|
A list that will be jsonify'd before being returned.
|
||||||
|
|
||||||
``generator`` or ``iterator``
|
``generator`` or ``iterator``
|
||||||
A generator that returns ``str`` or ``bytes`` to be
|
A generator that returns ``str`` or ``bytes`` to be
|
||||||
streamed as the response.
|
streamed as the response.
|
||||||
|
|
@ -1855,6 +1858,7 @@ class Flask(Scaffold):
|
||||||
|
|
||||||
.. versionchanged:: 2.2
|
.. versionchanged:: 2.2
|
||||||
A generator will be converted to a streaming response.
|
A generator will be converted to a streaming response.
|
||||||
|
A list will be converted to a JSON response.
|
||||||
|
|
||||||
.. versionchanged:: 1.1
|
.. versionchanged:: 1.1
|
||||||
A dict will be converted to a JSON response.
|
A dict will be converted to a JSON response.
|
||||||
|
|
@ -1907,7 +1911,7 @@ class Flask(Scaffold):
|
||||||
headers=headers, # type: ignore[arg-type]
|
headers=headers, # type: ignore[arg-type]
|
||||||
)
|
)
|
||||||
status = headers = None
|
status = headers = None
|
||||||
elif isinstance(rv, dict):
|
elif isinstance(rv, (dict, list)):
|
||||||
rv = jsonify(rv)
|
rv = jsonify(rv)
|
||||||
elif isinstance(rv, BaseResponse) or callable(rv):
|
elif isinstance(rv, BaseResponse) or callable(rv):
|
||||||
# evaluate a WSGI callable, or coerce a different response
|
# evaluate a WSGI callable, or coerce a different response
|
||||||
|
|
@ -1920,14 +1924,14 @@ class Flask(Scaffold):
|
||||||
raise TypeError(
|
raise TypeError(
|
||||||
f"{e}\nThe view function did not return a valid"
|
f"{e}\nThe view function did not return a valid"
|
||||||
" response. The return type must be a string,"
|
" response. The return type must be a string,"
|
||||||
" dict, tuple, Response instance, or WSGI"
|
" dict, list, tuple, Response instance, or WSGI"
|
||||||
f" callable, but it was a {type(rv).__name__}."
|
f" callable, but it was a {type(rv).__name__}."
|
||||||
).with_traceback(sys.exc_info()[2]) from None
|
).with_traceback(sys.exc_info()[2]) from None
|
||||||
else:
|
else:
|
||||||
raise TypeError(
|
raise TypeError(
|
||||||
"The view function did not return a valid"
|
"The view function did not return a valid"
|
||||||
" response. The return type must be a string,"
|
" response. The return type must be a string,"
|
||||||
" dict, tuple, Response instance, or WSGI"
|
" dict, list, tuple, Response instance, or WSGI"
|
||||||
f" callable, but it was a {type(rv).__name__}."
|
f" callable, but it was a {type(rv).__name__}."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ if t.TYPE_CHECKING: # pragma: no cover
|
||||||
|
|
||||||
# The possible types that are directly convertible or are a Response object.
|
# The possible types that are directly convertible or are a Response object.
|
||||||
ResponseValue = t.Union[
|
ResponseValue = t.Union[
|
||||||
"Response", str, bytes, t.Dict[str, t.Any], t.Iterator[str], t.Iterator[bytes]
|
"Response", str, bytes, list, t.Dict[str, t.Any], t.Iterator[str], t.Iterator[bytes]
|
||||||
]
|
]
|
||||||
|
|
||||||
# the possible types for an individual HTTP header
|
# the possible types for an individual HTTP header
|
||||||
|
|
|
||||||
|
|
@ -1166,6 +1166,10 @@ def test_response_types(app, client):
|
||||||
def from_dict():
|
def from_dict():
|
||||||
return {"foo": "bar"}, 201
|
return {"foo": "bar"}, 201
|
||||||
|
|
||||||
|
@app.route("/list")
|
||||||
|
def from_list():
|
||||||
|
return ["foo", "bar"], 201
|
||||||
|
|
||||||
assert client.get("/text").data == "Hällo Wörld".encode()
|
assert client.get("/text").data == "Hällo Wörld".encode()
|
||||||
assert client.get("/bytes").data == "Hällo Wörld".encode()
|
assert client.get("/bytes").data == "Hällo Wörld".encode()
|
||||||
|
|
||||||
|
|
@ -1205,6 +1209,10 @@ def test_response_types(app, client):
|
||||||
assert rv.json == {"foo": "bar"}
|
assert rv.json == {"foo": "bar"}
|
||||||
assert rv.status_code == 201
|
assert rv.status_code == 201
|
||||||
|
|
||||||
|
rv = client.get("/list")
|
||||||
|
assert rv.json == ["foo", "bar"]
|
||||||
|
assert rv.status_code == 201
|
||||||
|
|
||||||
|
|
||||||
def test_response_type_errors():
|
def test_response_type_errors():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue