Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs showing routes of all versions #34

Open
thetractor opened this issue Mar 8, 2021 · 3 comments
Open

Docs showing routes of all versions #34

thetractor opened this issue Mar 8, 2021 · 3 comments

Comments

@thetractor
Copy link

Hello,

I have a sample app with two versions of the user endpoint. Version 1 has an endpoint and version 2 has an other endpoint. Now if I navigate to the docs version 1 shows all correct but version two shows both of the endpoints together (the endpoint from v1 and the endpoint from v2)

Here my code:

# app/main.py
from fastapi import FastAPI
from fastapi_versioning import VersionedFastAPI

from app.v1.routers import users as users_v1
from app.v2.routers import users as users_v2

app = FastAPI()

app.include_router(users_v1.router)
app.include_router(users_v2.router)

app = VersionedFastAPI(app, version_format='{major}', prefix_format='/v{major}')
# app/v1/routers/users.py

from fastapi import APIRouter
from fastapi_versioning import versioned_api_route

router = APIRouter(route_class=versioned_api_route(1, 0))


@router.get("/users/v1", tags=["users"])
async def read_users():
    return [{"username": "Rick"}, {"username": "Morty"}]
# app/v2/routers/users.py

from fastapi import APIRouter
from fastapi_versioning import versioned_api_route

router = APIRouter(route_class=versioned_api_route(2, 0))


@router.get("/users/v2", tags=["users"])
async def read_users():
    return [{"username": "Rick"}, {"username": "Morty"}]

Here what I see in the docs when I navigate to http://localhost:8000/v2/docs
image

I would expect the docs of version 2 to only show the endpoint defined in app/v2/routers/users.py and not a combination of both versions.

Thanx for any help with this!

@crashCoder
Copy link

Hello,

I believe this is not an issue, there is a bit of misunderstanding of how it works. Let me explain:

Basically what you are doing with this code router = APIRouter(route_class=versioned_api_route(1, 0)) is generating a new version, version 1 to be precise.
The same happen with this one router = APIRouter(route_class=versioned_api_route(2, 0)), we create version 2.

The problem here is that you are specifying again version on the path e.g. "/users/v1" so this URL will look like this: http://localhost:8000/v1/users/v1

When you are adding the new route /users/v2, this is a completely different path as /users/v1 because remember, the versioning is already happening when we initialize APIRouter

When we create a new version all the routes from the past version will be replicated in the new version + the new routes with the new version

@colibri17
Copy link

colibri17 commented Jun 5, 2022

I am in the same situation. So there is no way to include in the new version all the routes of the new version + just the routes of the old version which are also referenced in the new one?

Up to now I ended up not to use fastapi_versioning at all, but creating separated APIRouter for the two versions.

@peauc
Copy link

peauc commented Jun 21, 2022

@colibri17 To fix this I had to patch the library. In the file fastapi_versioning/versioning.py I moved the line 50 to line 53 and it works flawlessly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants