'Different behavior Field in pydantic and SQLModel in OpenAPI spec
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
