mirror of
https://github.com/rjNemo/fastapi
synced 2026-06-11 13:06:43 +00:00
🐛 Fix support for strings in OpenAPI status codes: default, 1XX, 2XX, 3XX, 4XX, 5XX (#5187)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
parent
9c6086eee6
commit
0ae8db447a
2 changed files with 73 additions and 0 deletions
|
|
@ -21,6 +21,16 @@ if TYPE_CHECKING: # pragma: nocover
|
||||||
def is_body_allowed_for_status_code(status_code: Union[int, str, None]) -> bool:
|
def is_body_allowed_for_status_code(status_code: Union[int, str, None]) -> bool:
|
||||||
if status_code is None:
|
if status_code is None:
|
||||||
return True
|
return True
|
||||||
|
# Ref: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#patterned-fields-1
|
||||||
|
if status_code in {
|
||||||
|
"default",
|
||||||
|
"1XX",
|
||||||
|
"2XX",
|
||||||
|
"3XX",
|
||||||
|
"4XX",
|
||||||
|
"5XX",
|
||||||
|
}:
|
||||||
|
return True
|
||||||
current_status_code = int(status_code)
|
current_status_code = int(status_code)
|
||||||
return not (current_status_code < 200 or current_status_code in {204, 304})
|
return not (current_status_code < 200 or current_status_code in {204, 304})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,11 @@
|
||||||
from fastapi import APIRouter, FastAPI
|
from fastapi import APIRouter, FastAPI
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class ResponseModel(BaseModel):
|
||||||
|
message: str
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
@ -33,6 +39,18 @@ async def c():
|
||||||
return "c"
|
return "c"
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"/d",
|
||||||
|
responses={
|
||||||
|
"400": {"description": "Error with str"},
|
||||||
|
"5XX": {"model": ResponseModel},
|
||||||
|
"default": {"model": ResponseModel},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
async def d():
|
||||||
|
return "d"
|
||||||
|
|
||||||
|
|
||||||
app.include_router(router)
|
app.include_router(router)
|
||||||
|
|
||||||
openapi_schema = {
|
openapi_schema = {
|
||||||
|
|
@ -81,6 +99,45 @@ openapi_schema = {
|
||||||
"operationId": "c_c_get",
|
"operationId": "c_c_get",
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/d": {
|
||||||
|
"get": {
|
||||||
|
"responses": {
|
||||||
|
"400": {"description": "Error with str"},
|
||||||
|
"5XX": {
|
||||||
|
"description": "Server Error",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {"$ref": "#/components/schemas/ResponseModel"}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"200": {
|
||||||
|
"description": "Successful Response",
|
||||||
|
"content": {"application/json": {"schema": {}}},
|
||||||
|
},
|
||||||
|
"default": {
|
||||||
|
"description": "Default Response",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {"$ref": "#/components/schemas/ResponseModel"}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"summary": "D",
|
||||||
|
"operationId": "d_d_get",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"ResponseModel": {
|
||||||
|
"title": "ResponseModel",
|
||||||
|
"required": ["message"],
|
||||||
|
"type": "object",
|
||||||
|
"properties": {"message": {"title": "Message", "type": "string"}},
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -109,3 +166,9 @@ def test_c():
|
||||||
response = client.get("/c")
|
response = client.get("/c")
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
assert response.json() == "c"
|
assert response.json() == "c"
|
||||||
|
|
||||||
|
|
||||||
|
def test_d():
|
||||||
|
response = client.get("/d")
|
||||||
|
assert response.status_code == 200, response.text
|
||||||
|
assert response.json() == "d"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue