Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit73dbbea

Browse files
divumstiangolo
authored andcommitted
✨ Allow additional responses to use status ranges and "default" (#435)
1 parent417a3ab commit73dbbea

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

‎fastapi/openapi/utils.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@
4343
},
4444
}
4545

46+
status_code_ranges:Dict[str,str]= {
47+
"1XX":"Information",
48+
"2XX":"Success",
49+
"3XX":"Redirection",
50+
"4XX":"Client Error",
51+
"5XX":"Server Error",
52+
"default":"Default Response",
53+
}
54+
4655

4756
defget_openapi_params(dependant:Dependant)->List[Field]:
4857
flat_dependant=get_flat_dependant(dependant,skip_repeats=True)
@@ -190,12 +199,14 @@ def get_openapi_path(
190199
response.setdefault("content", {}).setdefault(
191200
"application/json", {}
192201
)["schema"]=response_schema
193-
status_text=http.client.responses.get(int(additional_status_code))
202+
status_text:Optional[str]=status_code_ranges.get(
203+
str(additional_status_code).upper()
204+
)orhttp.client.responses.get(int(additional_status_code))
194205
response.setdefault(
195206
"description",status_textor"Additional Response"
196207
)
197208
operation.setdefault("responses", {})[
198-
str(additional_status_code)
209+
str(additional_status_code).upper()
199210
]=response
200211
status_code=str(route.status_code)
201212
response_schema= {"type":"string"}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
importpytest
2+
fromfastapiimportFastAPI
3+
fromstarlette.testclientimportTestClient
4+
5+
app=FastAPI()
6+
7+
8+
@app.get("/a",responses={"hello": {"description":"Not a valid additional response"}})
9+
asyncdefa():
10+
pass# pragma: no cover
11+
12+
13+
openapi_schema= {
14+
"openapi":"3.0.2",
15+
"info": {"title":"Fast API","version":"0.1.0"},
16+
"paths": {
17+
"/a": {
18+
"get": {
19+
"responses": {
20+
# this is how one would imagine the openapi schema to be
21+
# but since the key is not valid, openapi.utils.get_openapi will raise ValueError
22+
"hello": {"description":"Not a valid additional response"},
23+
"200": {
24+
"description":"Successful Response",
25+
"content": {"application/json": {"schema": {}}},
26+
},
27+
},
28+
"summary":"A",
29+
"operationId":"a_a_get",
30+
}
31+
}
32+
},
33+
}
34+
35+
client=TestClient(app)
36+
37+
38+
deftest_openapi_schema():
39+
withpytest.raises(ValueError):
40+
client.get("/openapi.json")

‎tests/test_additional_responses_router.py‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,24 @@ async def a():
1010
return"a"
1111

1212

13-
@router.get("/b",responses={502: {"description":"Error 2"}})
13+
@router.get(
14+
"/b",
15+
responses={
16+
502: {"description":"Error 2"},
17+
"4XX": {"description":"Error with range, upper"},
18+
},
19+
)
1420
asyncdefb():
1521
return"b"
1622

1723

18-
@router.get("/c",responses={501: {"description":"Error 3"}})
24+
@router.get(
25+
"/c",
26+
responses={
27+
"400": {"description":"Error with str"},
28+
"5xx": {"description":"Error with range, lower"},
29+
},
30+
)
1931
asyncdefc():
2032
return"c"
2133

@@ -43,6 +55,7 @@ async def c():
4355
"get": {
4456
"responses": {
4557
"502": {"description":"Error 2"},
58+
"4XX": {"description":"Error with range, upper"},
4659
"200": {
4760
"description":"Successful Response",
4861
"content": {"application/json": {"schema": {}}},
@@ -55,7 +68,8 @@ async def c():
5568
"/c": {
5669
"get": {
5770
"responses": {
58-
"501": {"description":"Error 3"},
71+
"400": {"description":"Error with str"},
72+
"5XX": {"description":"Error with range, lower"},
5973
"200": {
6074
"description":"Successful Response",
6175
"content": {"application/json": {"schema": {}}},

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp