question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Routes defined with same APIRouter instance share responses

See original GitHub issue

Describe 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: example1

Example 2: (different routers) example2

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mostaphaRoudsaricommented, Apr 5, 2019

That was quick! Fixed the issue for me. Thanks.

1reaction
tiangolocommented, Apr 5, 2019

Thanks for checking it so fast!

Great bug report.

It should be fixed in #140 , the fix is released in version 0.12.1. 🎉

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found