I am building a REST API with FastAPI and I really like the tool, especially the integration with Pydantic and OpenAPI.
For instance I can write the model taken by an endpoint as
class Model(BaseModel):
field1: str = Field(default=...)
field2: int = Field(default=...)
field3: List[int] = Field(default=...)
field4: float = Field(default=...)
class Config:
schema_extra = {
"example": {
'field1': 'example 1',
'field2': 1,
'field3': [1, 2],
'field4': 1.3,
}
}
I find however that I have quite some repetitions in my code as for example if I want to create another class inheriting from Model that adds another field field5, I would need to re-write the Config class in order to define the new example.
Is there a good way of doing this? For example, is there any tool that allows you to define the fields with all the properties and example and then creating the class Model from a definition of what it requires to include?
Or any other pattern that is better suited for this is also welcome.