python
config_error
ai_generated
true
断言错误:重复的路由:/items
AssertionError: Duplicate route: /items
ID: python/fastapi-duplicate-route
80%修复率
85%置信度
0证据数
2024-04-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
FastAPI 不允许为同一 HTTP 方法和路径定义两个路由处理程序。
English
FastAPI does not allow defining two route handlers for the same HTTP method and path.
解决方案
-
100% 成功率 Remove or comment out the duplicate route
# Remove the second @app.get('/items') decorator -
100% 成功率 Use different HTTP methods or paths
@app.get('/items') def list_items(): ... @app.post('/items') def create_item(): ...
无效尝试
常见但无效的做法:
-
Renaming one function without changing the route
100% 失败
The route path and method are still duplicated.
-
Adding a prefix to one route manually
80% 失败
If the prefix doesn't change the full path, duplication persists.