脚本宝典收集整理的这篇文章主要介绍了FastAPI(15)- 声明请求示例数据,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
可以使用 config cass 和 schema_extra 为 Pydantic Model 声明一个示例值
From typing import Optional import uvicorn from pydantic import BaSEModel from fastapi import FastAPI app = FastAPI() class ITem(BaseModel): name: str description: Optional[str] = None PRice: float tax: Optional[float] = None # 内部类,固定写法 class Config: schema_extra = { "example": { "name": "Foo", "description": "A very nice Item", "price": 35.4, "tax": 3.2, } } @app.put("/items/{item_id}") async def update_item(item_id: int, item: Item): results = {"item_id": item_id, "item": item} return results if __name__ == "__main__": uvicorn.run(app="13_example:app", host="127.0.0.1", port=8080, reload=True, debug=True)
无论是 Example Value 还是 Schema 都会显示声明的示例值
使用 Pydantic 的 Field() 时,可以将任何其他任意参数添加到函数参数中,来声明 JSON Schema 的额外信息
默认 Field 是没有 example 参数的,而 **extra 就是关键字参数,表示可以添加其他任意参数,和常见的 **kwargs 是一个作用哦
关键字参数教程
Field 教程
#!usr/bin/env python # -*- coding:utf-8 _*- """ # author: 小菠萝测试笔记 # blog: https://www.cnbLOGs.COM/poloyy/ # time: 2021/9/19 9:40 下午 # file: 12_model.py """ from typing import Optional import uvicorn from pydantic import BaseModel, Field from fastapi import FastAPI app = FastAPI() class Item(BaseModel): # 给每个字段加上了 example 参数 name: str = Field(..., example="小菠萝") description: Optional[str] = Field(None, example="描述") price: float = Field(..., example=1.11) tax: Optional[float] = Field(None, example=3.2) @app.put("/items/{item_id}") async def update_item(item_id: int, item: Item): results = {"item_id": item_id, "item": item} return results
它是针对每个字段设置的示例值,所以会显示在字段下
当使用 FastAPI 提供的
可以声明一个 example 或 examples 参数,FastAPI 会自动将 example、examples 的值添加到 OpenAPI 文档中
Pydantic 并没有直接支持 example 参数,而 FastAPI 进行了扩展,直接支持添加 example、examples 参数
#!usr/bin/env python # -*- coding:utf-8 _*- """ # author: 小菠萝测试笔记 # blog: https://www.cnblogs.com/poloyy/ # time: 2021/9/19 9:40 下午 # file: 12_model.py """ from typing import Optional import uvicorn from pydantic import BaseModel from fastapi import FastAPI, Body 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( default=..., description="描述", # 添加一个 example 参数 example={ "name": "body name", "description": "body 描述", "price": 3.33, "tax": 5.55 }, ), ): results = {"item_id": item_id, "item": item} return results if __name__ == "__main__": uvicorn.run(app="13_example:app", host="127.0.0.1", port=8080, reload=True, debug=True)
Schema 并不会显示 example 的值哦
本身是一个 dict,每个键标识一个具体的示例,而键对应的值也是一个 dict
每个示例 dict 可以包含
#!usr/bin/env python # -*- coding:utf-8 _*- """ # author: 小菠萝测试笔记 # blog: https://www.cnblogs.com/poloyy/ # time: 2021/9/19 9:40 下午 # file: 12_model.py """ from typing import Optional import uvicorn from pydantic import BaseModel, Field from fastapi import FastAPI, Body 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( default=..., # 三个键,代表三个不一样的示例值 examples={ "normal": { "summary": "正常的栗子", "description": "A **normal** item works correctly.", "value": { "name": "Foo", "description": "A very nice Item", "price": 35.4, "tax": 3.2, }, }, "converted": { "summary": "会自动转换类型的栗子", "description": "FastAPI can convert price `strings` to actual `numbers` automatically", "value": { "name": "Bar", "price": "35.4", }, }, "invalid": { "summary": "校验失败的栗子", "value": { "name": "Baz", "price": "thirty five point four", }, }, }, ), ): results = {"item_id": item_id, "item": item} return results if __name__ == "__main__": uvicorn.run(app="13_example:app", host="127.0.0.1", port=8080, reload=True, debug=True)
以上是脚本宝典为你收集整理的FastAPI(15)- 声明请求示例数据全部内容,希望文章能够帮你解决FastAPI(15)- 声明请求示例数据所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。