Skip to content
Myr

Support returning list as JSON

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Grey Li · committed by David Lord · parent 1626aff · Browse files · 3 files+16 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+16 added−4 removed

  • tests:+8 added−0 removed
  • other:+8 added−4 removed

3 files+16 added−4 removed

modified:src/flask/app.py+7 added-3 removed
1828 unmodified lines
18291830 ``dict``1831 A dictionary that will be jsonify'd before being returned.18321833 ``list``1834 A list that will be jsonify'd before being returned.18351836 ``generator`` or ``iterator``1837 A generator that returns ``str`` or ``bytes`` to be1838 streamed as the response.1839
17 unmodified lines
1857 used to create a response object.18581859 .. versionchanged:: 2.21860 A generator will be converted to a streaming response.1861 A list will be converted to a JSON response.18621863 .. versionchanged:: 1.11864 A dict will be converted to a JSON response.1865
44 unmodified lines
1910 status=status,1911 headers=headers, # type: ignore[arg-type]1912 )1913 status = headers = None1910 elif isinstance(rv, dict):1914 elif isinstance(rv, (dict, list)):1915 rv = jsonify(rv)1916 elif isinstance(rv, BaseResponse) or callable(rv):1917 # evaluate a WSGI callable, or coerce a different response1918 # class to the correct type
4 unmodified lines
1923 except TypeError as e:1924 raise TypeError(1925 f"{e}\nThe view function did not return a valid"1926 " response. The return type must be a string,"1923 " dict, tuple, Response instance, or WSGI"1927 " dict, list, tuple, Response instance, or WSGI"1928 f" callable, but it was a {type(rv).__name__}."1929 ).with_traceback(sys.exc_info()[2]) from None1930 else:1931 raise TypeError(1932 "The view function did not return a valid"1933 " response. The return type must be a string,"1930 " dict, tuple, Response instance, or WSGI"1934 " dict, list, tuple, Response instance, or WSGI"1935 f" callable, but it was a {type(rv).__name__}."1936 )19371938 rv = t.cast(Response, rv)
370 unmodified lines
modified:src/flask/typing.py+1 added-1 removed
5 unmodified lines
6 from werkzeug.wrappers import Response # noqa: F40178# The possible types that are directly convertible or are a Response object.9ResponseValue = t.Union[10 "Response", str, bytes, t.Dict[str, t.Any], t.Iterator[str], t.Iterator[bytes]10 "Response", str, bytes, list, t.Dict[str, t.Any], t.Iterator[str], t.Iterator[bytes]11]1213# the possible types for an individual HTTP header14# This should be a Union, but mypy doesn't pass unless it's a TypeVar.
46 unmodified lines
modified:tests/test_basic.py+8 added
1164 unmodified lines
1165 @app.route("/dict")1166 def from_dict():1167 return {"foo": "bar"}, 20111681169 @app.route("/list")1170 def from_list():1171 return ["foo", "bar"], 20111721173 assert client.get("/text").data == "Hällo Wörld".encode()1174 assert client.get("/bytes").data == "Hällo Wörld".encode()11751176 rv = client.get("/full_tuple")
31 unmodified lines
1208 rv = client.get("/dict")1209 assert rv.json == {"foo": "bar"}1210 assert rv.status_code == 20112111212 rv = client.get("/list")1213 assert rv.json == ["foo", "bar"]1214 assert rv.status_code == 201121512161217def test_response_type_errors():1218 app = flask.Flask(__name__)1219 app.testing = True1220
761 unmodified lines