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

fix: mix up of routes with different major versions #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
26 changes: 26 additions & 0 deletions example/major_version_only/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from fastapi import FastAPI

from fastapi_versioning import VersionedFastAPI, version

app = FastAPI(title="My Online Store")


@app.get("/item")
@version(1, 0)
def home_v1() -> str:
return "Hello item version 1.0!"


@app.get("/other")
@version(1, 1)
def home_v1() -> str:
return "Hello other version 1.1!"


@app.get("/another")
@version(2, 0)
def home_v2() -> str:
return "Hello another version 2.0!"


app = VersionedFastAPI(app)
4 changes: 3 additions & 1 deletion fastapi_versioning/versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def VersionedFastAPI(
]

for version, route in version_routes:
route.version = version
version_route_mapping[version].append(route)

unique_routes = {}
Expand All @@ -63,7 +64,8 @@ def VersionedFastAPI(
for method in route.methods:
unique_routes[route.path + "|" + method] = route
for route in unique_routes.values():
versioned_app.router.routes.append(route)
if major == route.version[0]:
versioned_app.router.routes.append(route)
parent_app.mount(prefix, versioned_app)

@parent_app.get(
Expand Down
15 changes: 15 additions & 0 deletions tests/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from example.annotation.app import app as annotation_app
from example.custom_default_version.app import app as default_version_app
from example.router.app import app as router_app
from example.major_version_only.app import app as major_version_only_app


def test_annotation_app() -> None:
Expand Down Expand Up @@ -87,3 +88,17 @@ def test_default_version() -> None:

assert test_client.get("/v2_0/").json() == "Hello default version 2.0!"
assert test_client.get("/v3_0/").json() == "Hello version 3.0!"


def test_major_version_has_not_previous_major() -> None:
test_client = TestClient(major_version_only_app)

assert test_client.get("/v1_0/item").json() == "Hello item version 1.0!"
assert test_client.get("/v1_1/item").json() == "Hello item version 1.0!"
assert test_client.get("/v1_1/other").json() == "Hello other version 1.1!"
assert test_client.get("/v2_0/another").json() == "Hello another version 2.0!"

assert test_client.get("/v1_0/another").status_code == 404
assert test_client.get("/v1_1/another").status_code == 404
assert test_client.get("/v2_0/item").status_code == 404
assert test_client.get("/v2_0/other").status_code == 404