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

Commit2b47f3e

Browse files
✨ Add support for adding OpenAPI schema for GET requests with a body (#1626)
* add test for get request body's openapi schema* 📝 Update docs note for GET requests with body* ✅ Update test for GET request with body, test it receives the body* 🔇 Temporary type ignore while it's handled in PydanticCo-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
1 parentd60dd1b commit2b47f3e

File tree

4 files changed

+116
-4
lines changed

4 files changed

+116
-4
lines changed

‎docs/en/docs/tutorial/body.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ Your API almost always has to send a **response** body. But clients don't necess
99
To declare a**request** body, you use <ahref="https://pydantic-docs.helpmanual.io/"class="external-link"target="_blank">Pydantic</a> models with all their power and benefits.
1010

1111
!!! info
12-
You cannotsenda request body using a`GET` operation (HTTP method).
12+
Tosenddata, you should use one of:`POST` (the more common),`PUT`,`DELETE` or`PATCH`.
1313

14-
To send data, you have to use one of: `POST` (the more common), `PUT`, `DELETE` or `PATCH`.
14+
Sending a body with a `GET` request has an undefined behavior in the specifications, nevertheless, it is supported by FastAPI, only for very complex/extreme use cases.
15+
16+
As it is discouraged, the interactive docs with Swagger UI won't show the documentation for the body when using `GET`, and proxies in the middle might not support it.
1517

1618
##Import Pydantic's`BaseModel`
1719

‎fastapi/dependencies/utils.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ def get_typed_signature(call: Callable) -> inspect.Signature:
246246
defget_typed_annotation(param:inspect.Parameter,globalns:Dict[str,Any])->Any:
247247
annotation=param.annotation
248248
ifisinstance(annotation,str):
249-
annotation=ForwardRef(annotation)
249+
# Temporary ignore type
250+
# Ref: https://github.com/samuelcolvin/pydantic/issues/1738
251+
annotation=ForwardRef(annotation)# type: ignore
250252
annotation=evaluate_forwardref(annotation,globalns,globalns)
251253
returnannotation
252254

‎fastapi/openapi/constants.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
METHODS_WITH_BODY=set(("POST","PUT","DELETE","PATCH"))
1+
METHODS_WITH_BODY=set(("GET","HEAD","POST","PUT","DELETE","PATCH"))
22
STATUS_CODES_WITH_NO_BODY=set((100,101,102,103,204,304))
33
REF_PREFIX="#/components/schemas/"

‎tests/test_get_request_body.py‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
fromfastapiimportFastAPI
2+
fromfastapi.testclientimportTestClient
3+
frompydanticimportBaseModel
4+
5+
app=FastAPI()
6+
7+
8+
classProduct(BaseModel):
9+
name:str
10+
description:str=None
11+
price:float
12+
13+
14+
@app.get("/product")
15+
asyncdefcreate_item(product:Product):
16+
returnproduct
17+
18+
19+
client=TestClient(app)
20+
21+
22+
openapi_schema= {
23+
"openapi":"3.0.2",
24+
"info": {"title":"FastAPI","version":"0.1.0"},
25+
"paths": {
26+
"/product": {
27+
"get": {
28+
"summary":"Create Item",
29+
"operationId":"create_item_product_get",
30+
"requestBody": {
31+
"content": {
32+
"application/json": {
33+
"schema": {"$ref":"#/components/schemas/Product"}
34+
}
35+
},
36+
"required":True,
37+
},
38+
"responses": {
39+
"200": {
40+
"description":"Successful Response",
41+
"content": {"application/json": {"schema": {}}},
42+
},
43+
"422": {
44+
"description":"Validation Error",
45+
"content": {
46+
"application/json": {
47+
"schema": {
48+
"$ref":"#/components/schemas/HTTPValidationError"
49+
}
50+
}
51+
},
52+
},
53+
},
54+
}
55+
}
56+
},
57+
"components": {
58+
"schemas": {
59+
"HTTPValidationError": {
60+
"title":"HTTPValidationError",
61+
"type":"object",
62+
"properties": {
63+
"detail": {
64+
"title":"Detail",
65+
"type":"array",
66+
"items": {"$ref":"#/components/schemas/ValidationError"},
67+
}
68+
},
69+
},
70+
"Product": {
71+
"title":"Product",
72+
"required": ["name","price"],
73+
"type":"object",
74+
"properties": {
75+
"name": {"title":"Name","type":"string"},
76+
"description": {"title":"Description","type":"string"},
77+
"price": {"title":"Price","type":"number"},
78+
},
79+
},
80+
"ValidationError": {
81+
"title":"ValidationError",
82+
"required": ["loc","msg","type"],
83+
"type":"object",
84+
"properties": {
85+
"loc": {
86+
"title":"Location",
87+
"type":"array",
88+
"items": {"type":"string"},
89+
},
90+
"msg": {"title":"Message","type":"string"},
91+
"type": {"title":"Error Type","type":"string"},
92+
},
93+
},
94+
}
95+
},
96+
}
97+
98+
99+
deftest_openapi_schema():
100+
response=client.get("/openapi.json")
101+
assertresponse.status_code==200,response.text
102+
assertresponse.json()==openapi_schema
103+
104+
105+
deftest_get_with_body():
106+
body= {"name":"Foo","description":"Some description","price":5.5}
107+
response=client.get("/product",json=body)
108+
assertresponse.json()==body

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp