python runtime_error ai_generated true

AssertionError: Duplicate path parameter: 'item_id'

ID: python/fastapi-route-duplicate-path

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
0Evidence
2024-03-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

FastAPI路由中定义了相同名称的路径参数,导致参数冲突

generic

中文

FastAPI路由中定义了相同名称的路径参数,导致参数冲突

Workarounds

  1. 95% success 重命名其中一个路径参数
    @app.get('/items/{item_id}')
    @app.get('/items/{other_id}')
  2. 90% success 合并为单个路由并处理不同逻辑
    @app.get('/items/{item_id}')
    async def read_item(item_id: int): pass

Dead Ends

Common approaches that don't work:

  1. 尝试更改路由顺序而不修改参数名 50% fail

    参数名重复是根本问题,顺序无关

  2. 使用不同的HTTP方法但保留相同参数名 30% fail

    参数名冲突发生在同一路由组内