FastAPI Path()的gt与lt参数在Swagger UI中不生效问题咨询
FastAPI路径参数
gt/lt约束不生效且Swagger UI不显示问题 使用版本
- python 3.9.9
- FastAPI 0.95.1
- uvicorn 0.22.0
- firefox 102.8.0
问题现象
在以下代码中,路径参数item_id通过gt=100和lt=1000设置了大于100且小于1000的范围约束,但该约束在Swagger UI中完全不显示,同时实际请求时也不生效。与之对比,使用ge(大于等于)和le(小于等于)参数时,约束既能在Swagger UI中正常展示,也能正确生效。
示例代码
from typing import Annotated from fastapi import FastAPI, Path app = FastAPI() @app.get("/items/{item_id}") async def read_items( item_id: Annotated[int, Path(title="The ID of the item to get", gt=100, lt=1000)] ): return {"item_id": item_id}
内容的提问来源于stack exchange,提问作者jung rhew
相关产品推荐
相关产品推荐

