33from typing import Any
44
55from starlette .types import ASGIApp , Message , Receive , Scope , Send
6+ from starlette .requests import Request
67
78from ..config import EndpointMethods
89from ..utils .requests import dict_to_bytes
@@ -21,20 +22,30 @@ class OpenApiMiddleware:
2122
2223 async def __call__ (self , scope : Scope , receive : Receive , send : Send ) -> None :
2324 """Add the OpenAPI spec to the response."""
24- if scope ["type" ] != "http" :
25+ if scope ["type" ] != "http" or Request ( scope ). url . path != self . openapi_spec_path :
2526 return await self .app (scope , receive , send )
2627
28+ total_body = b""
29+
2730 async def augment_oidc_spec (message : Message ):
2831 if message ["type" ] != "http.response.body" :
2932 return await send (message )
3033
3134 # TODO: Make more robust to handle non-JSON responses
32- body = json .loads (message ["body" ])
35+
36+ nonlocal total_body
37+
38+ total_body += message ["body" ]
39+
40+ # Pass empty body chunks until all chunks have been received
41+ if message ["more_body" ]:
42+ return await send ({** message , "body" : b"" })
3343
3444 await send (
3545 {
3646 "type" : "http.response.body" ,
37- "body" : dict_to_bytes (self .augment_spec (body )),
47+ "body" : dict_to_bytes (self .augment_spec (json .loads (total_body ))),
48+ "more_body" : False ,
3849 }
3950 )
4051
0 commit comments