Repository navigation
Replies: 2 comments 2 replies
|
To fix your code example you need to:
from fastapi import FastAPI
app = FastAPI()
@app.get("/toto/{tata_int:int}")
def titi(tata_int: int) -> int:
return tata_int + tata_int
@app.get("/toto/{tata_str:str}")
def tutu(tata_str: str) -> str:
return tata_str + tata_str |
2 replies
This comment was marked as spam.
This comment was marked as spam.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
First Check
Commit to Help
Example Code
Description
fastapi devhttp://127.0.0.1:8000/docs/toto/{tata}is documented withtiti(int parameter, int response) — the last registered route42): it raisesRuntimeErrorbecause the first registered route (tutu) is the one that matches at runtimeOperating System
macOS
Operating System Details
No response
FastAPI Version
0.135.1
Pydantic Version
2.12.5
Python Version
Python 3.13.12
Additional Context
Summary
When two routes share the same path template but use different Starlette path converters (e.g.
{tata:str}and{tata:int}), the runtime router and the OpenAPI spec pick different handlers:/toto/{tata}; dict assignment overwrites the firstThis means the Swagger UI documents behavior that cannot be reached at runtime.
Expected behavior
The OpenAPI spec and the runtime router should agree. Either:
Actual behavior
titi(int → int)tutu(str → raises)The user sees a working endpoint in the docs but gets an error when calling it.
Related discussions
Discussion #6255 covers routing based on path parameter types and suggests reordering routes as a workaround. However, it does not address the fact that the OpenAPI spec and the runtime router disagree on which handler is selected when two routes collapse to the same path template.
All reactions