Browse Source

Fix: preserve stream_item_type when using include_router for SSE routes

pull/15435/head
Prashanth1602 3 months ago
parent
commit
1a4d3e4a37
  1. 8
      fastapi/routing.py
  2. 17
      tests/test_sse.py

8
fastapi/routing.py

@ -1794,6 +1794,14 @@ class APIRouter(routing.Router):
self.strict_content_type,
),
)
new_route = self.routes[-1]
if (
isinstance(new_route, APIRoute)
and isinstance(route, APIRoute)
and getattr(route, "stream_item_type", None) is not None
):
new_route.stream_item_type = route.stream_item_type
elif isinstance(route, routing.Route):
methods = list(route.methods or [])
self.add_route(

17
tests/test_sse.py

@ -264,6 +264,23 @@ def test_sse_on_router_included_in_app(client: TestClient):
]
assert len(data_lines) == 2
def test_include_router_copies_stream_item_type():
sub_router = APIRouter()
@sub_router.get("/stream_item_type", response_class=EventSourceResponse)
async def stream_item_type_route() -> AsyncIterable[Item]:
yield items[0]
assert getattr(sub_router.routes[-1], "stream_item_type", None) == Item
main_app = FastAPI()
main_app.include_router(sub_router)
route = next(r for r in main_app.routes
if r.path == "/stream_item_type")
assert getattr(route, "stream_item_type", None) == Item
# Keepalive ping tests

Loading…
Cancel
Save