在这两种情况下,我都在方法参数中看到了 Depends()。当参数中没有任何内容时,Depends 会做什么?
这是一个很好的问题。
假设您有以下代码。
from fastapi import FastAPI, Depends
from pydantic import BaseModel
from typing import Optional
class Location(BaseModel):
city: str
country: str
state: str
app = FastAPI()
@app.post("/withoutdepends")
async def with_depends(location: Location):
return location
@app.post("/withdepends")
async def with_depends(location: Location = Depends()):
return lcoation
我们在两个不同的端点上有相同的Location 模型,一个使用Depends,另一个没有。
有什么区别?
由于 FastAPI 基于 OpenAPI 规范,我们可以开始发现与自动生成的 Swagger 文档的不同之处。
这是没有 Depends,它需要一个请求正文。
这是with Depends,它期望它们作为查询参数。
这有什么用处以及它是如何工作的?
其实就是这样,它期望那里有一个Callable。
但是当您使用带有Depends 的Pydantic 模型 时,它实际上会为init __init__ 函数中的参数创建一个查询参数。
例如,这是我们上面使用的模型。
class Location(BaseModel):
city: str
country: str
state: str
用Depends变成这个。
class Location(BaseModel):
def __init__(self, city: str, country: str, state: str) -> None:
...
然后它们将成为查询参数。这是 /withdepends 端点的 OpenAPI 架构。
"parameters": [
{
"required":true,
"schema":{
"title":"City",
"type":"string"
},
"name":"city",
"in":"query"
},
{
"required":true,
"schema":{
"title":"Country",
"type":"string"
},
"name":"country",
"in":"query"
},
{
"required":true,
"schema":{
"title":"State",
"type":"string"
},
"name":"state",
"in":"query"
}
]
这是它为 /withoutdepends 端点创建的 OpenAPI 架构。
"requestBody": {
"content":{
"application/json":{
"schema":{
"$ref":"#/components/schemas/Location"
}
}
},
"required":true
}
结论
您可以使用相同的模型创建查询参数,而不是请求正文。
Pydantic 模型在您有 +5 个参数的情况下非常有用。但它默认需要一个请求正文。但是OpenAPI specification 不允许 GET 操作中的请求正文。正如规范中所说的那样。
GET、DELETE 和 HEAD 不再允许有请求正文,因为它没有按照 RFC 7231 定义的语义。
因此,通过使用 Depends,您可以使用相同的模型为您的 GET 端点创建查询参数。