cal*_*ani 10 python swagger-ui openapi fastapi
我正在关注 FastAPI 教程,特别是有关显示多个示例的部分: https: //fastapi.tiangolo.com/tutorial/schema-extra-example/。
我复制了代码:
from typing import Optional
from fastapi import Body, FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
@app.put("/items/{item_id}")
async def update_item(
*,
item_id: int,
item: Item = Body(
...,
examples={
"normal": {
"summary": "A normal example",
"description": "A **normal** item works correctly.",
"value": {
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
},
"converted": {
"summary": "An example with converted data",
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
"value": {
"name": "Bar",
"price": "35.4",
},
},
"invalid": {
"summary": "Invalid data is rejected with an error",
"value": {
"name": "Baz",
"price": "thirty five point four",
},
},
},
),
):
results = {"item_id": item_id, "item": item}
return results
Run Code Online (Sandbox Code Playgroud)
...与页面上显示的完全一样,我使用 Uvicorn 运行它。在我的屏幕上,我没有看到请求正文示例字典中包含 3 个示例的任何下拉菜单。我所看到的只是一个示例值,其中包含字段所需的类型。
为什么会这样,为什么这对我不起作用?
这就是我应该得到的:
但这就是我得到的:
Srđ*_*pić 12
我不确定这是否有帮助: https://github.com/tiangolo/fastapi/pull/1267#issuecomment-762557261 这意味着两件事:
Item.Config.schema_extra像这样的东西:
class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
class Config:
schema_extra = {
"examples": {
"normal": {
"summary": "A normal example",
"description": "A **normal** item works correctly.",
"value": {
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
},
"converted": {
"summary": "An example with converted data",
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
"value": {
"name": "Bar",
"price": "35.4",
},
},
"invalid": {
"summary": "Invalid data is rejected with an error",
"value": {
"name": "Baz",
"price": "thirty five point four",
},
},
}
}
@app.put("/items/{item_id}")
async def update_item(
*,
item_id: int,
item: Item = Body(
None,
examples=Item.Config.schema_extra["examples"],
),
):
results = {"item_id": item_id, "item": item}
return results
Run Code Online (Sandbox Code Playgroud)
它帮助了我!
| 归档时间: |
|
| 查看次数: |
4540 次 |
| 最近记录: |