mirror of https://github.com/tiangolo/fastapi.git
Merge 68a08572c1 into 0127069d47
This commit is contained in:
commit
f89aeb1163
|
|
@ -337,6 +337,7 @@ def get_openapi_path(
|
||||||
)
|
)
|
||||||
callbacks[callback.name] = {callback.path: cb_path}
|
callbacks[callback.name] = {callback.path: cb_path}
|
||||||
operation["callbacks"] = callbacks
|
operation["callbacks"] = callbacks
|
||||||
|
status_code = "200"
|
||||||
if route.status_code is not None:
|
if route.status_code is not None:
|
||||||
status_code = str(route.status_code)
|
status_code = str(route.status_code)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,62 @@
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
from inline_snapshot import snapshot
|
||||||
|
from starlette.responses import Response
|
||||||
|
|
||||||
|
|
||||||
|
class CustomResponse(Response):
|
||||||
|
"""A custom response class whose __init__ does not expose status_code
|
||||||
|
as a parameter with an int default. This previously caused an
|
||||||
|
UnboundLocalError during OpenAPI schema generation when the route
|
||||||
|
did not specify an explicit status_code."""
|
||||||
|
|
||||||
|
media_type = "text/plain"
|
||||||
|
|
||||||
|
def __init__(self, content: str, **kwargs: object) -> None:
|
||||||
|
super().__init__(content=content, status_code=200, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/items", response_class=CustomResponse)
|
||||||
|
async def get_items():
|
||||||
|
return "ok"
|
||||||
|
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_response():
|
||||||
|
response = client.get("/items")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.text == "ok"
|
||||||
|
|
||||||
|
|
||||||
|
def test_openapi_schema():
|
||||||
|
"""OpenAPI generation must not crash when the response class's __init__
|
||||||
|
lacks a status_code parameter with an int default."""
|
||||||
|
response = client.get("/openapi.json")
|
||||||
|
assert response.status_code == 200, response.text
|
||||||
|
assert response.json() == snapshot(
|
||||||
|
{
|
||||||
|
"openapi": "3.1.0",
|
||||||
|
"info": {"title": "FastAPI", "version": "0.1.0"},
|
||||||
|
"paths": {
|
||||||
|
"/items": {
|
||||||
|
"get": {
|
||||||
|
"summary": "Get Items",
|
||||||
|
"operationId": "get_items_items_get",
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Successful Response",
|
||||||
|
"content": {
|
||||||
|
"text/plain": {"schema": {"type": "string"}}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
Loading…
Reference in New Issue