Browse Source

Add isinstance guard when restoring stream attrs in include_router

self.routes[-1] is typed as Starlette's BaseRoute, which lacks the
stream_item_type/stream_item_field/app attributes used by APIRoute.
Narrow the type with an isinstance check so mypy is satisfied and the
access is defensively guarded even though the preceding
isinstance(route, APIRoute) plus route_class_override=type(route) make
it true in practice.
pull/15426/head
rzsultan-boop 2 months ago
parent
commit
137f8f46d7
  1. 7
      fastapi/routing.py

7
fastapi/routing.py

@ -1802,9 +1802,10 @@ class APIRouter(routing.Router):
# must restore both and re-bake the handler.
if route.stream_item_type is not None:
new_route = self.routes[-1]
new_route.stream_item_type = route.stream_item_type
new_route.stream_item_field = route.stream_item_field
new_route.app = request_response(new_route.get_route_handler())
if isinstance(new_route, APIRoute):
new_route.stream_item_type = route.stream_item_type
new_route.stream_item_field = route.stream_item_field
new_route.app = request_response(new_route.get_route_handler())
elif isinstance(route, routing.Route):
methods = list(route.methods or [])
self.add_route(

Loading…
Cancel
Save