Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed python 3.8.7 compatibility #2798

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion fastapi/responses.py
Expand Up @@ -7,17 +7,29 @@
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 orjson
except ImportError: # pragma: nocover
orjson = None # type: ignore

try:
import ujson
except ImportError: # pragma: nocover
ujson = None # type: ignore


class ORJSONResponse(JSONResponse):
media_type = "application/json"

def render(self, content: Any) -> bytes:
assert orjson is not None, "orjson must be installed to use ORJSONResponse"
return orjson.dumps(content)


class UJSONResponse(JSONResponse):
media_type = "application/json"

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")
3 changes: 2 additions & 1 deletion pyproject.toml
Expand Up @@ -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"
Expand All @@ -56,6 +56,7 @@ test = [
"sqlalchemy >=1.3.18,<2.0.0",
"peewee >=3.13.3,<4.0.0",
"databases[sqlite] >=0.3.2,<0.4.0",
"ujson >=3.0.0,<4.0.0",
"orjson >=3.2.1,<4.0.0",
"async_exit_stack >=1.0.1,<2.0.0",
"async_generator >=1.10,<2.0.0",
Expand Down
36 changes: 36 additions & 0 deletions tests/test_tutorial/test_custom_response/test_tutorial001.py
@@ -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"}]