Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
JarroVGIT committed Jul 25, 2022
2 parents 7fe8158 + 85013ce commit 97e9dcb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
12 changes: 11 additions & 1 deletion fastapi/utils.py
Expand Up @@ -21,7 +21,17 @@
def is_body_allowed_for_status_code(status_code: Union[int, str, None]) -> bool:
if status_code is None:
return True
if status_code in ['default', '2XX', '2xx', '3XX', '3xx', '4XX', '4xx', '5XX', '5xx']:
if status_code in [
"default",
"2XX",
"2xx",
"3XX",
"3xx",
"4XX",
"4xx",
"5XX",
"5xx",
]:
return True
current_status_code = int(status_code)
return not (current_status_code < 200 or current_status_code in {204, 304})
Expand Down
31 changes: 21 additions & 10 deletions tests/test_additional_responses_router.py
Expand Up @@ -2,9 +2,11 @@
from fastapi.testclient import TestClient
from pydantic import BaseModel


class ResponseModel(BaseModel):
message: str


app = FastAPI()
router = APIRouter()

Expand Down Expand Up @@ -36,6 +38,7 @@ async def b():
async def c():
return "c"


@router.get(
"/d",
responses={
Expand All @@ -47,6 +50,7 @@ async def c():
async def d():
return "d"


app.include_router(router)

openapi_schema = {
Expand Down Expand Up @@ -94,41 +98,47 @@ async def d():
"summary": "C",
"operationId": "c_c_get",
}
},
},
"/d": {
"get": {
"responses": {
"400": {"description": "Error with str"},
"5XX": {
"description": "Server Error",
"content": {"application/json": {"schema": {
"$ref": "#/components/schemas/ResponseModel"}}}
"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"}}}
}
"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"}}
"properties": {"message": {"title": "Message", "type": "string"}},
}
}
}
},
}

client = TestClient(app)
Expand Down Expand Up @@ -157,7 +167,8 @@ def test_c():
assert response.status_code == 200, response.text
assert response.json() == "c"


def test_d():
response = client.get("/d")
assert response.status_code == 200, response.text
assert response.json() == "d"
assert response.json() == "d"

0 comments on commit 97e9dcb

Please sign in to comment.