diff -Naur fastapi-0.63.0-original/fastapi/responses.py fastapi-0.63.0/fastapi/responses.py
--- fastapi-0.63.0-original/fastapi/responses.py 2020-12-20 14:20:34.172180000 -0500
+++ fastapi-0.63.0/fastapi/responses.py 2021-03-04 15:17:03.600186189 -0500
@@ -7,7 +7,11 @@
from starlette.responses import RedirectResponse as RedirectResponse # noqa
from starlette.responses import Response as Response # noqa
from starlette.responses import StreamingResponse as StreamingResponse # noqa
-from starlette.responses import UJSONResponse as UJSONResponse # noqa
+
+try:
+ import ujson
+except ImportError: # pragma: nocover
+ ujson = None # type: ignore
try:
import orjson
@@ -15,6 +19,12 @@
orjson = None # type: ignore
+class UJSONResponse(JSONResponse):
+ def render(self, content: Any) -> bytes:
+ assert ujson is not None, "ujson must be installed to use UJSONResponse"
+ return ujson.dumps(content, ensure_ascii=False).encode("utf-8")
+
+
class ORJSONResponse(JSONResponse):
media_type = "application/json"
diff -Naur fastapi-0.63.0-original/pyproject.toml fastapi-0.63.0/pyproject.toml
--- fastapi-0.63.0-original/pyproject.toml 2020-12-20 14:20:34.176180000 -0500
+++ fastapi-0.63.0/pyproject.toml 2021-03-04 15:18:37.563775376 -0500
@@ -32,7 +32,7 @@
"Topic :: Internet :: WWW/HTTP",
]
requires = [
- "starlette ==0.13.6",
+ "starlette ==0.14.1",
"pydantic >=1.0.0,<2.0.0"
]
description-file = "README.md"
@@ -57,6 +57,7 @@
"peewee >=3.13.3,<4.0.0",
"databases[sqlite] >=0.3.2,<0.4.0",
"orjson >=3.2.1,<4.0.0",
+ "ujson >=4.0.1,<5.0.0",
"async_exit_stack >=1.0.1,<2.0.0",
"async_generator >=1.10,<2.0.0",
"python-multipart >=0.0.5,<0.0.6",
@@ -87,7 +88,7 @@
"itsdangerous >=1.1.0,<2.0.0",
"pyyaml >=5.3.1,<6.0.0",
"graphene >=2.1.8,<3.0.0",
- "ujson >=3.0.0,<4.0.0",
+ "ujson >=4.0.1,<5.0.0",
"orjson >=3.2.1,<4.0.0",
"email_validator >=1.1.1,<2.0.0",
"uvicorn[standard] >=0.12.0,<0.14.0",
diff -Naur fastapi-0.63.0-original/tests/test_tutorial/test_custom_response/test_tutorial001.py fastapi-0.63.0/tests/test_tutorial/test_custom_response/test_tutorial001.py
--- fastapi-0.63.0-original/tests/test_tutorial/test_custom_response/test_tutorial001.py 1969-12-31 19:00:00.000000000 -0500
+++ fastapi-0.63.0/tests/test_tutorial/test_custom_response/test_tutorial001.py 2021-03-04 15:18:51.213860966 -0500
@@ -0,0 +1,36 @@
+from fastapi.testclient import TestClient
+
+from docs_src.custom_response.tutorial001 import app
+
+client = TestClient(app)
+
+openapi_schema = {
+ "openapi": "3.0.2",
+ "info": {"title": "FastAPI", "version": "0.1.0"},
+ "paths": {
+ "/items/": {
+ "get": {
+ "responses": {
+ "200": {
+ "description": "Successful Response",
+ "content": {"application/json": {"schema": {}}},
+ }
+ },
+ "summary": "Read Items",
+ "operationId": "read_items_items__get",
+ }
+ }
+ },
+}
+
+
+def test_openapi_schema():
+ response = client.get("/openapi.json")
+ assert response.status_code == 200, response.text
+ assert response.json() == openapi_schema
+
+
+def test_get_custom_response():
+ response = client.get("/items/")
+ assert response.status_code == 200, response.text
+ assert response.json() == [{"item_id": "Foo"}]