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
Provenance
- Commit
ca2bfbb- Tree
096d8f9- Author
Grey Li <withlihui@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
1626aff
3 files changed+16 added−4 removed
modified:src/flask/app.py+7 added-3 removed
1828 unmodified lines1829 1830 ``dict``1831 A dictionary that will be jsonify'd before being returned.1832 1833+ ``list``1834+ A list that will be jsonify'd before being returned.1835+1836 ``generator`` or ``iterator``1837 A generator that returns ``str`` or ``bytes`` to be1838 streamed as the response.1839 17 unmodified lines1857 used to create a response object.1858 1859 .. versionchanged:: 2.21860 A generator will be converted to a streaming response.1861+ A list will be converted to a JSON response.1862 1863 .. versionchanged:: 1.11864 A dict will be converted to a JSON response.1865 44 unmodified lines1910 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 type4 unmodified lines1923 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 )1937 1938 rv = t.cast(Response, rv)370 unmodified linesmodified:src/flask/typing.py+1 added-1 removed
5 unmodified lines6 from werkzeug.wrappers import Response # noqa: F4017 8 # The possible types that are directly convertible or are a Response object.9 ResponseValue = 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 ]12 13 # 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 linesmodified:tests/test_basic.py+8 added
1164 unmodified lines1165 @app.route("/dict")1166 def from_dict():1167 return {"foo": "bar"}, 2011168 1169+ @app.route("/list")1170+ def from_list():1171+ return ["foo", "bar"], 2011172+1173 assert client.get("/text").data == "Hällo Wörld".encode()1174 assert client.get("/bytes").data == "Hällo Wörld".encode()1175 1176 rv = client.get("/full_tuple")31 unmodified lines1208 rv = client.get("/dict")1209 assert rv.json == {"foo": "bar"}1210 assert rv.status_code == 2011211 1212+ rv = client.get("/list")1213+ assert rv.json == ["foo", "bar"]1214+ assert rv.status_code == 2011215 1216+1217 def test_response_type_errors():1218 app = flask.Flask(__name__)1219 app.testing = True1220 761 unmodified lines