fastapi-utils
fastapi-utils copied to clipboard
[BUG] Router's prefix is added twice when using CBV
Describe the bug
When I use CBV with a router that has a prefix, the prefix is included twice.
from fastapi import APIRouter
from fastapi_utils.cbv import cbv
router = APIRouter(prefix='/api/v1')
@cbv(router)
class C:
@router.get('')
def f(self):
...
assert router.routes[-1].path == '/api/v1/api/v1'
>>> print(fastapi_utils.__version__)
0.2.1
>>> print(fastapi.__version__)
0.63.0
This is because the path already has a prefix before CBV removes and re-adds it to a router:
-
@router.get
callsrouter.add_api_route
which adds a prefix to the path. -
cbv
callsrouter.include_router(cbv_router)
which again callsrouter.add_api_route
which adds a prefix to the path.
One solution would be to not remove and re-add routes here, and only change the signature, so instead of
for route in cbv_routes:
router.routes.remove(route)
_update_cbv_route_endpoint_signature(cls, route)
cbv_router.routes.append(route)
router.include_router(cbv_router)
do
for route in cbv_routes:
_update_cbv_route_endpoint_signature(cls, route)
@dmontagu I'm willing to submit a PR with tests if it's okay with you
Describe the bug
When I use CBV with a router that has a prefix, the prefix is included twice.
from fastapi import APIRouter from fastapi_utils.cbv import cbv router = APIRouter(prefix='/api/v1') @cbv(router) class C: @router.get('') def f(self): ... assert router.routes[-1].path == '/api/v1/api/v1'
>>> print(fastapi_utils.__version__) 0.2.1 >>> print(fastapi.__version__) 0.63.0
This is because the path already has a prefix before CBV removes and re-adds it to a router:
1. `@router.get` calls `router.add_api_route` which adds a prefix to the path. 2. `cbv` calls `router.include_router(cbv_router)` which again calls `router.add_api_route` which adds a prefix to the path.
One solution would be to not remove and re-add routes here, and only change the signature, so instead of
for route in cbv_routes: router.routes.remove(route) _update_cbv_route_endpoint_signature(cls, route) cbv_router.routes.append(route) router.include_router(cbv_router)
do
for route in cbv_routes: _update_cbv_route_endpoint_signature(cls, route)
I think this also fixes #85
Perhaps you could make a PR to fastapi_restful, a fork of this project, and is possibly more actively maintained: https://github.com/yuval9313/fastapi-restful/?
I did confirm that this issue also exists in fastapi-restful.
It took some time but as @falkben said I had this PR merged in my fork feel free to check it out :)
Any update on this? Thanks
Any update on this? Thanks
Consider using the forked and maintained version of this lib: https://github.com/yuval9313/FastApi-RESTful. They've already fixed this issue.