mirror of https://github.com/tiangolo/fastapi.git
⬆️ Upgrade Starlette to 0.14.2, including internal UJSONResponse migrated from Starlette (#2335)
This commit is contained in:
parent
04ac466748
commit
4aed0411e9
|
|
@ -7,7 +7,12 @@ from starlette.responses import PlainTextResponse as PlainTextResponse # noqa
|
||||||
from starlette.responses import RedirectResponse as RedirectResponse # noqa
|
from starlette.responses import RedirectResponse as RedirectResponse # noqa
|
||||||
from starlette.responses import Response as Response # noqa
|
from starlette.responses import Response as Response # noqa
|
||||||
from starlette.responses import StreamingResponse as StreamingResponse # 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:
|
try:
|
||||||
import orjson
|
import orjson
|
||||||
|
|
@ -15,6 +20,12 @@ except ImportError: # pragma: nocover
|
||||||
orjson = None # type: ignore
|
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):
|
class ORJSONResponse(JSONResponse):
|
||||||
media_type = "application/json"
|
media_type = "application/json"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ classifiers = [
|
||||||
"Topic :: Internet :: WWW/HTTP",
|
"Topic :: Internet :: WWW/HTTP",
|
||||||
]
|
]
|
||||||
requires = [
|
requires = [
|
||||||
"starlette ==0.13.6",
|
"starlette ==0.14.2",
|
||||||
"pydantic >=1.0.0,<2.0.0"
|
"pydantic >=1.0.0,<2.0.0"
|
||||||
]
|
]
|
||||||
description-file = "README.md"
|
description-file = "README.md"
|
||||||
|
|
@ -57,6 +57,7 @@ test = [
|
||||||
"peewee >=3.13.3,<4.0.0",
|
"peewee >=3.13.3,<4.0.0",
|
||||||
"databases[sqlite] >=0.3.2,<0.4.0",
|
"databases[sqlite] >=0.3.2,<0.4.0",
|
||||||
"orjson >=3.2.1,<4.0.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_exit_stack >=1.0.1,<2.0.0",
|
||||||
"async_generator >=1.10,<2.0.0",
|
"async_generator >=1.10,<2.0.0",
|
||||||
"python-multipart >=0.0.5,<0.0.6",
|
"python-multipart >=0.0.5,<0.0.6",
|
||||||
|
|
@ -87,7 +88,7 @@ all = [
|
||||||
"itsdangerous >=1.1.0,<2.0.0",
|
"itsdangerous >=1.1.0,<2.0.0",
|
||||||
"pyyaml >=5.3.1,<6.0.0",
|
"pyyaml >=5.3.1,<6.0.0",
|
||||||
"graphene >=2.1.8,<3.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",
|
"orjson >=3.2.1,<4.0.0",
|
||||||
"email_validator >=1.1.1,<2.0.0",
|
"email_validator >=1.1.1,<2.0.0",
|
||||||
"uvicorn[standard] >=0.12.0,<0.14.0",
|
"uvicorn[standard] >=0.12.0,<0.14.0",
|
||||||
|
|
|
||||||
|
|
@ -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"}]
|
||||||
Loading…
Reference in New Issue