python config_error ai_generated true

断言错误:重复的路由:/items

AssertionError: Duplicate route: /items

ID: python/fastapi-duplicate-route

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

generic

解决方案

  1. 100% 成功率 Remove or comment out the duplicate route
    # Remove the second @app.get('/items') decorator
  2. 100% 成功率 Use different HTTP methods or paths
    @app.get('/items')
    def list_items(): ...
    @app.post('/items')
    def create_item(): ...

无效尝试

常见但无效的做法:

  1. Renaming one function without changing the route 100% 失败

    The route path and method are still duplicated.

  2. Adding a prefix to one route manually 80% 失败

    If the prefix doesn't change the full path, duplication persists.