Hello everyone.
Today I tried to use fastAPI to add an exemple of values for a model, as decribed here.
After a tedious research in the documentation of Pydantic, I managed to go from this implementation:
from fastapi import FastAPI
from pydantic import BaseModel
class CustomModel(BaseModel):
id: str
name: str
app = FastAPI()
@app.get("/", response_model=CustomModel)
def root() -> CustomModel:
return {"message": "Hello World"}
Giving the following result:

To this implementation:
from fastapi import FastAPI
from pydantic import BaseModel
class CustomModel(BaseModel):
id: str
name: str
class Config(object):
schema_extra = {
'example': {
'id': 'n3xR4Ib79h',
'name': 'John Doe'
}
}
app = FastAPI()
@app.get("/", response_model=CustomModel)
def root() -> CustomModel:
return {"message": "Hello World"}
Giving the following result:

The code used above is introduced in this page of pydantic's documentation.
As this is a behaviour documented in OpenAPI, and as fastAPI can actually provide that behaviour, should it be a good idea to add this to the doc ? It is quite hidden inside pydantic docs, and it could be usefull to other users.
Thank you for your opinion,
Simon
Hello everyone.
Today I tried to use fastAPI to add an exemple of values for a model, as decribed here.
After a tedious research in the documentation of Pydantic, I managed to go from this implementation:
Giving the following result:

To this implementation:
Giving the following result:

The code used above is introduced in this page of pydantic's documentation.
As this is a behaviour documented in OpenAPI, and as fastAPI can actually provide that behaviour, should it be a good idea to add this to the doc ? It is quite hidden inside pydantic docs, and it could be usefull to other users.
Thank you for your opinion,
Simon