如何使用FastApi在OpenAPI/Swagger中记录默认的无/null?
使用ORM,我想执行一个发布请求,让一些具有 null
值的字段,该字段将在数据库中翻译成指定的默认值。
问题是OpenApi(Swagger) Docs ,忽略默认的 none
,并且默认情况下仍会提示 uuid
。
from fastapi import FastAPI
from pydantic import BaseModel
from typing import Optional
from uuid import UUID
import uvicorn
class Table(BaseModel):
# ID: Optional[UUID] # the docs show a example UUID, ok
ID: Optional[UUID] = None # the docs still shows a uuid, when it should show a null or valid None value.
app = FastAPI()
@app.post("/table/", response_model=Table)
def create_table(table: Table):
# here we call to sqlalchey orm etc.
return 'nothing important, the important thing is in the docs'
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
在 docs 上的OpenAPI架构示例(请求正文)中,我们发现:
{
"ID": "3fa85f64-5717-4562-b3fc-2c963f66afa6"
}
这不是可以的,因为我指定默认值是 none
,所以我期望这一点相反:
{
"ID": null, # null is the equivalent of None here
}
它将将 null
传递给 ID
,最后将在DB中解析为默认值(这是新生成的 uuid
)。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您声明
可选
参数时,用户不应在用null
或 none 的请求中包含这些参数在python中),为了无
。 默认情况下,此类参数的值将为none
,除非用户在发送请求时指定其他值。因此,您所要做的就是使用
config
和schema_extra
来声明pydantic模型的自定义示例
,如文档,如下所示。下面的示例将创建一个空的(IE,{}
)在OpenAPI(Swagger UI)中的请求正文,可以成功提交(因为ID
是唯一的属性模型,是可选的)。如果
表
模型包含了一些其他必需的属性,则可以为这些属性添加示例
值,如下所示:如果您想保留其余属性的自动生成示例 除外,除了
id> id
属性,您可以使用以下内容删除id
来自生成架构中模型的属性(灵感来自 schema> ):另外,如果您想将自定义
示例
添加到某些属性,则可以使用field()
(如所述 s e there );例如,some_attr:str = field(example =“ foo”)
。另一个可能的解决方案是修改生成的OpenAPI模式,如
注意
ID:可选[UUID] =无
与id:uuid = none
相同。如前所述,如Fastapi网站(请参阅此答案):从那以后,Fastapi修改了他们的 documentation :
因此,
id:union [uuid,none] = none
是 same 与ID:可选[uuid] = none
和id :uuid =无
。在 python 3.10+中,也可以使用id:uuid | none = none
(请参阅在这里) 。按照(请参阅提供的链接中的
info
部分):When you declare
Optional
parameters, users shouldn't include those parameters in the request specified withnull
orNone
(in Python), in order to beNone
. By default, the value of such parameters will beNone
, unless the user specifies some other value when sending the request.Hence, all you have to do is to declare a custom
example
for the Pydantic model usingConfig
andschema_extra
, as described in the documentation and as shown below. The below example will create an empty (i.e.,{}
) request body in OpenAPI (Swagger UI), which can be successfully submitted (asID
is the only attribute of the model and is optional).If the
Table
model included some other required attributes, you could addexample
values for those, as demonstrated below:If you would like to keep the auto-generated examples for the rest of the attributes except the one for the
ID
attribute, you could use the below to removeID
from the model's properties in the generated schema (inspired by Schema customization):Also, if you would like to add custom
example
to some of the attributes, you could useField()
(as described here); for example,some_attr: str = Field(example="Foo")
.Another possible solution would be to modify the generated OpenAPI schema, as described in Solution 3 of this answer. Though, the above solution is likely more suited to this case.
Note
ID: Optional[UUID] = None
is the same asID: UUID = None
. As previously documented in FastAPI website (see this answer as well):Since then, FastAPI has revised their documentation with the following:
Hence,
ID: Union[UUID, None] = None
is the same asID: Optional[UUID] = None
andID: UUID = None
. In Python 3.10+, one could also useID: UUID| None = None
(see here).As per FastAPI documentation (see
Info
section in the link provided):