Skip to content
Snippets Groups Projects
Commit e75739d2 authored by DAVID Axel's avatar DAVID Axel
Browse files

fix: :bug: Select routes only for the good api version :bug:

parent bdb43025
Branches
Tags
No related merge requests found
......@@ -20,11 +20,10 @@ from typing import final, Final
from blacksheep.server.openapi.ui import ReDocUIProvider
from blacksheep.server.openapi.v3 import OpenAPIHandler
from openapidocs.common import Format
from openapidocs.v3 import Contact, ExternalDocs, Info, License, OpenAPI, Tag
from vigenere_api.version import Version
from vigenere_api.version import Version
from .errors import VersionTypeError
from .open_api_route_filter import get_route_filter
......@@ -81,7 +80,10 @@ class VigenereAPIOpenAPIHandler(OpenAPIHandler):
)
self.ui_providers.append(ReDocUIProvider(ui_path=f"{api_route}/redocs"))
self.include = get_route_filter((api_route, json_spec_path, yaml_spec_path))
self.include = get_route_filter(
(api_route, json_spec_path, yaml_spec_path),
version,
)
@property
def version(self) -> Version:
......
......@@ -20,10 +20,19 @@ from collections.abc import Callable, Collection
from blacksheep import Route
from .errors import ExcludedPathsTypeError, ExcludedPathTypeError, PathTypeError
def get_route_filter(excluded: Collection[str]) -> Callable[[str, Route], bool]:
from vigenere_api.version import Version
from .errors import (
ExcludedPathsTypeError,
ExcludedPathTypeError,
PathTypeError,
VersionTypeError,
)
def get_route_filter(
excluded: Collection[str],
api_version: Version,
) -> Callable[[str, Route], bool]:
"""
Get the route filter.
......@@ -31,6 +40,8 @@ def get_route_filter(excluded: Collection[str]) -> Callable[[str, Route], bool]:
----------
excluded : Collection[str]
All routes that should not be exposed.
api_version : Version
The API version.
Raises
------
......@@ -38,6 +49,8 @@ def get_route_filter(excluded: Collection[str]) -> Callable[[str, Route], bool]:
Thrown if 'excluded' is not a Collection.
ExcludedPathTypeError
Thrown if 'excluded' does not contain only str.
VersionTypeError
Thrown if 'api_version' is not a Version.
Returns
-------
......@@ -51,6 +64,9 @@ def get_route_filter(excluded: Collection[str]) -> Callable[[str, Route], bool]:
if not isinstance(excluded_path, str):
raise ExcludedPathTypeError(excluded_path, excluded)
if not isinstance(api_version, Version):
raise VersionTypeError(api_version)
def _route_filter(path: str, _: Route) -> bool:
"""
Exclude routes that should not be exposed.
......@@ -80,6 +96,6 @@ def get_route_filter(excluded: Collection[str]) -> Callable[[str, Route], bool]:
if not isinstance(path, str):
raise PathTypeError(path)
return path not in excluded
return path not in excluded and path.startswith(f"/api/v{api_version.major}")
return _route_filter
......@@ -22,13 +22,16 @@ from vigenere_api.api.helpers.errors import (
ExcludedPathsTypeError,
ExcludedPathTypeError,
PathTypeError,
VersionTypeError,
)
from vigenere_api.api.helpers.open_api_route_filter import get_route_filter
from vigenere_api.version import Version
def test_get_filter() -> None:
excluded = []
filter = get_route_filter(excluded)
version = Version(major=1, minor=1, patch=10)
filter = get_route_filter(excluded, version)
assert callable(filter)
s = signature(filter)
......@@ -45,31 +48,42 @@ def test_get_filter() -> None:
@pytest.mark.raises(exception=ExcludedPathsTypeError)
def test_bad_type_excluded() -> None:
excluded = 10
_ignored = get_route_filter(excluded)
version = Version(major=1, minor=1, patch=10)
_ignored = get_route_filter(excluded, version)
@pytest.mark.raises(exception=ExcludedPathTypeError)
def test_bad_type_path_in_excluded() -> None:
excluded = [b"test"]
_ignored = get_route_filter(excluded)
version = Version(major=1, minor=1, patch=10)
_ignored = get_route_filter(excluded, version)
@pytest.mark.raises(exception=VersionTypeError)
def test_bad_type_version() -> None:
_ignored = get_route_filter((), "1.0.0")
@pytest.mark.raises(exception=PathTypeError)
def test_filter_bad_type_path() -> None:
route_filter = get_route_filter(["/api"])
version = Version(major=1, minor=1, patch=10)
route_filter = get_route_filter(["/api"], version)
_ignored = route_filter(b"/http", Route("http", {}))
def test_filter_bad_type_route() -> None:
route_filter = get_route_filter(["/api"])
assert route_filter("/http", {})
version = Version(major=1, minor=1, patch=10)
route_filter = get_route_filter(["/api"], version)
assert route_filter(f"/api/v{version.major}/test", {})
def test_filter_route_excluded() -> None:
route_filter = get_route_filter(["/api"])
version = Version(major=1, minor=1, patch=10)
route_filter = get_route_filter(["/api"], version)
assert not route_filter("/api", Route("http", {}))
def test_filter_route_not_excluded() -> None:
route_filter = get_route_filter(["/api"])
assert route_filter("/http", Route("http", {}))
version = Version(major=1, minor=1, patch=10)
route_filter = get_route_filter(["/api"], version)
assert route_filter(f"/api/v{version.major}/test", Route("http", {}))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment