python config_error ai_generated true

FastAPI错误:函数中参数名重复

fastapi.exceptions.FastAPIError: Duplicate parameter name: 'path' in function 'catch_all'

ID: python/fastapi-route-order-matters

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. 90% 成功率
    Define specific routes before catch-all routes, and ensure catch-all uses a distinct parameter name like `{path:path}` with a converter.
  2. 85% 成功率
    Use `@app.api_route('/{path}', methods=['GET'], include_in_schema=False)` for catch-all to avoid conflicts.

无效尝试

常见但无效的做法:

  1. 80% 失败

    Reordering routes doesn't fix the duplicate parameter error if the catch-all is too broad.

  2. 75% 失败

    Using a different parameter name in the catch-all still conflicts with other routes.