python
config_error
ai_generated
true
ValueError: Duplicate path operation: GET /items/{item_id}
ID: python/fastapi-path-operation-conflict
80%Fix Rate
88%Confidence
0Evidence
2024-07-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
FastAPI中定义了多个相同HTTP方法和路径的路由。
generic中文
FastAPI中定义了多个相同HTTP方法和路径的路由。
Workarounds
-
95% success 确保每个路径和方法组合唯一
@app.get('/items/{item_id}') async def read_item(item_id: int): return {'item_id': item_id}; @app.post('/items/{item_id}') async def update_item(item_id: int): return {'updated': item_id} -
90% success 使用不同的路径或添加版本前缀
@app.get('/v1/items/{item_id}') async def read_item_v1(item_id: int): return {'item_id': item_id}
Dead Ends
Common approaches that don't work:
-
尝试覆盖路由而不删除旧路由
90% fail
FastAPI不允许重复注册。
-
使用不同的函数名但相同的路径和方法
80% fail
路径和方法是唯一标识。