python
config_error
ai_generated
true
FastAPI错误:函数中参数名重复
fastapi.exceptions.FastAPIError: Duplicate parameter name: 'path' in function 'catch_all'
ID: python/fastapi-route-order-matters
80%修复率
84%置信度
0证据数
2024-05-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
在特定路由之后定义通配路由(如`@app.get('/{path}')`)可能导致FastAPI错误解释路径参数,引发重复参数错误或错误匹配。
English
Defining a catch-all route like `@app.get('/{path}')` after specific routes can cause FastAPI to interpret path parameters incorrectly, leading to duplicate parameter errors or wrong matching.
解决方案
-
90% 成功率
Define specific routes before catch-all routes, and ensure catch-all uses a distinct parameter name like `{path:path}` with a converter. -
85% 成功率
Use `@app.api_route('/{path}', methods=['GET'], include_in_schema=False)` for catch-all to avoid conflicts.
无效尝试
常见但无效的做法:
-
80% 失败
Reordering routes doesn't fix the duplicate parameter error if the catch-all is too broad.
-
75% 失败
Using a different parameter name in the catch-all still conflicts with other routes.