]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
⬆️ Upgrade Starlette to 0.14.2, including internal UJSONResponse migrated from Starl...
authorHannes Küttner <kuettner.hannes@gmail.com>
Mon, 10 May 2021 14:09:04 +0000 (16:09 +0200)
committerGitHub <noreply@github.com>
Mon, 10 May 2021 14:09:04 +0000 (16:09 +0200)
fastapi/responses.py
pyproject.toml
tests/test_tutorial/test_custom_response/test_tutorial001.py [new file with mode: 0644]

index 8d9d62dfb00683b511540d1fa1b2b28a4b685c10..6cd7931571fc9ed2b4e53839fc68f7783d58e0f4 100644 (file)
@@ -7,7 +7,12 @@ from starlette.responses import PlainTextResponse as PlainTextResponse  # noqa
 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 +20,12 @@ except ImportError:  # pragma: nocover
     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"
 
index 66cb00345b60848df94bf0b08fb25eab28cff314..a2845ff26f668e896eeefeb07b6e8b9d70a5c4f3 100644 (file)
@@ -32,7 +32,7 @@ classifiers = [
     "Topic :: Internet :: WWW/HTTP",
 ]
 requires = [
-    "starlette ==0.13.6",
+    "starlette ==0.14.2",
     "pydantic >=1.0.0,<2.0.0"
 ]
 description-file = "README.md"
@@ -57,6 +57,7 @@ test = [
     "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 @@ all = [
     "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 --git a/tests/test_tutorial/test_custom_response/test_tutorial001.py b/tests/test_tutorial/test_custom_response/test_tutorial001.py
new file mode 100644 (file)
index 0000000..430076f
--- /dev/null
@@ -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"}]