-
First Check
Commit to Help
DescriptionIn my use-case, for specific environments we want to keep Swagger for quick API calls. But - users being users, don't mind thinking and click like monkeys. So we end up with bunch of fields with "string" as a content. Is there a quick way to overwrite all the examples, that are being generated into swagger file - dynamically? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
You can edit the resulting openapi spec, though I am not sure I would describe it as "quick" https://fastapi.tiangolo.com/how-to/extending-openapi/#extending-openapi |
Beta Was this translation helpful? Give feedback.
-
|
For now I came up with this: class ExampleModel(pydantic.BaseModel):
model_config: ConfigDict = {
"json_schema_extra": {
"examples": ({} if True else None)
}
}
...But a global solution would be appreciated. Leaving this open till then. |
Beta Was this translation helpful? Give feedback.
-
|
Please have a look at this to see if it can help you def json_schema_extra(schema: JsonDict, model: type[BaseModel]):
print(schema)
for name, prop in schema["properties"].items():
if name == "kind":
prop["examples"] = [model.__name__]
return schema
class Model(BaseModel):
kind: str = Field(examples=["Foo"])
model_config = ConfigDict(json_schema_extra=json_schema_extra)
class Pagination(Model):
limit: int
total_page: int |
Beta Was this translation helpful? Give feedback.
You can edit the resulting openapi spec, though I am not sure I would describe it as "quick"
https://fastapi.tiangolo.com/how-to/extending-openapi/#extending-openapi