Fastapi: [BUG] Routes defined with same APIRouter instance share responses

Created on 5 Apr 2019  路  5Comments  路  Source: tiangolo/fastapi

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

bug

Most helpful comment

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

All 5 comments

Thanks for checking it so fast!

Great bug report.

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

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

Great! Thanks for reporting @mostaphaRoudsari .

Yup, seems to be patched in 0.12.1馃憣

Great. Thanks for reporting back and closing the issue.

Was this page helpful?
0 / 5 - 0 ratings