OpenAPI 规范中 pydantic 和 SQLModel 中的不同行为字段
据我所知,SQLmodel
部分基于 pydantic
。尽管如此,当在 FastAPI
中使用 SQLmodel
中的 Field
函数时,我得到了不同的结果 OpenAPI 规范。
例如,请查看以下示例:
from typing import Optional
from fastapi import FastAPI
from sqlmodel import Field, SQLModel
app = FastAPI()
class Model(SQLModel):
nullable_param: Optional[int] = Field(None, nullable=True)
@app.get("/test", response_model=Model)
def foo():
return Model(nullable_param=3)
此代码运行,并且 test
端点上的 GET
正确返回 {"nullable_param":3}
。但是,生成的 Model
的 OpenAPI
规范如下:
{
"Model":{
"title":"Model",
"type":"object",
"properties":{
"nullable_param":{"title":"Nullable Param","type":"integer"}
}
}
}
请注意,nullable_param
缺少 nullable=true
属性!
当我直接从 pydantic
导入 Field
时(from pydantic import Field
),它确实包含此属性:
{
"Model":{
"title":"Model",
"type":"object",
"properties":{
"nullable_param":{ "title":"Nullable Param", "type":"integer", "nullable":true }
}
}
}
我是吗做错了什么,或者 FastAPI
上下文中 SQLModel
的 Field
实现可能存在错误?
尾端 1.8.2 快速API 0.74.1 sqlmodel 0.0.6 蟒蛇3.8.12
To my knowledge, SQLmodel
is partially based on pydantic
. Nevertheless, I get a different resulting OpenAPI specification when using the Field
function from SQLmodel
in FastAPI
.
For example, look at the following example:
from typing import Optional
from fastapi import FastAPI
from sqlmodel import Field, SQLModel
app = FastAPI()
class Model(SQLModel):
nullable_param: Optional[int] = Field(None, nullable=True)
@app.get("/test", response_model=Model)
def foo():
return Model(nullable_param=3)
This code runs and a GET
on the test
endpoint correctly returns {"nullable_param":3}
. However, the resulting OpenAPI
specification for Model
is the following:
{
"Model":{
"title":"Model",
"type":"object",
"properties":{
"nullable_param":{"title":"Nullable Param","type":"integer"}
}
}
}
Note that nullable_param
lacks the nullable=true
attribute!
When I import Field
directly from pydantic
(from pydantic import Field
), it does include this attribute:
{
"Model":{
"title":"Model",
"type":"object",
"properties":{
"nullable_param":{ "title":"Nullable Param", "type":"integer", "nullable":true }
}
}
}
Am I doing something wrong, or is there perhaps a bug in the Field
implementation of SQLModel
in a FastAPI
context?
pydantic 1.8.2
fastapi 0.74.1
sqlmodel 0.0.6
python 3.8.12
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论