[BUG] Routes defined with same APIRouter instance share responses
See original GitHub issueDescribe the bug When defining routes with an API router their alternative responses are shared.
To Reproduce Example 1:
from fastapi import FastAPI
from fastapi import APIRouter
app = FastAPI()
router = APIRouter()
@router.get("/a", responses={501: {"description": "Error 1"}})
async def a():
return "a"
@router.get("/b", responses={502: {"description": "Error 2"}})
async def b():
return "b"
@router.get("/c", responses={501: {"description": "Error 1 overwriten"}})
async def c():
return "c"
app.include_router(router)
/a will now have a single 501 error as expected. /b will now have both a 501 and a 502 error (should only be 502). /c will also have both 501 and 502, but an overwritten description (should only be 501).
If you split them into different routers, the problem does not occur: Example 2:
router1 = APIRouter()
router2 = APIRouter()
router3 = APIRouter()
@router1.get("/a", responses={501: {"description": "Error 1"}})
async def a():
return "a"
@router2.get("/b", responses={502: {"description": "Error 2"}})
async def b():
return "b"
@router3.get("/c", responses={501: {"description": "Error 1 overwriten"}})
async def c():
return "c"
app.include_router(router1)
app.include_router(router2)
app.include_router(router3
Expected behavior Responses for different paths should not be merged. This only occurs for APIRouter, not if paths are added directly to the FastAPI app instance.
Screenshots Example 1:
Example 2: (different routers)
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Bigger Applications - Multiple Files - FastAPI
You can think of APIRouter as a "mini FastAPI " class. All the same options are supported. All the same parameters , responses...
Read more >React-router URLs don't work when refreshing or writing ...
Basically you define a route for /* and make it respond with your HTML page. The tricky thing here is to make sure...
Read more >Frequently Asked Questions Regarding BGP - Cisco
What does r RIB-Failure mean in the show ip bgp command output? How can I redistribute internal BGP (iBGP) learned default-route (0.0.0.0/0) route...
Read more >5 Advanced Features of FastAPI You Should Try
Following the same rules of mounting different WSGI applications for different routes, you can also mount different FastAPI applications within the FastAPI ...
Read more >Scala Routing - 2.8.x - Play Framework
Routes are defined in the conf/routes file, which is compiled. ... Play's default routes generator creates a router class that accepts controller instances...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That was quick! Fixed the issue for me. Thanks.
Thanks for checking it so fast!
Great bug report.
It should be fixed in #140 , the fix is released in version
0.12.1
. 🎉