Skip to content

[BUG] Router's prefix is added twice when using CBV #154

Description

@WouldYouKindly

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions