1111from starlette .routing import BaseRoute , Match
1212from starlette .types import Receive , Scope , Send
1313
14+ from cadwyn ._internal .context_vars import DEFAULT_API_VERSION_VAR
1415from cadwyn ._utils import same_definition_as_in
1516from cadwyn .middleware import APIVersionFormat
1617from cadwyn .structure .common import VersionType
@@ -70,8 +71,8 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
7071 if scope ["type" ] == "lifespan" :
7172 await self .lifespan (scope , receive , send )
7273 return
73-
7474 version = self .api_version_var .get (None )
75+ default_version_that_was_picked = DEFAULT_API_VERSION_VAR .get (None )
7576
7677 # if version is None, then it's an unversioned request and we need to use the unversioned routes
7778 # if there will be a value, we search for the most suitable version
@@ -81,6 +82,14 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
8182 routes = self .versioned_routers [version ].routes
8283 else :
8384 routes = await self ._get_routes_from_closest_suitable_version (version )
85+ if default_version_that_was_picked :
86+ # We add unversioned routes to versioned routes because otherwise unversioned routes
87+ # will be completely unavailable when a default version is passed. So routes such as
88+ # /docs will not be accessible at all.
89+
90+ # We use this order because if versioned routes go first and there is a versioned route that is
91+ # the same as an unversioned route -- the unversioned one becomes impossible to match.
92+ routes = self .unversioned_routes + routes
8493 await self .process_request (scope = scope , receive = receive , send = send , routes = routes )
8594
8695 @cached_property
0 commit comments