fix: CachedJSONResponse overwrites body after init

This commit is contained in:
g0ldyy
2026-01-09 22:47:47 +01:00
parent e74c29fdd1
commit 72bb8859a7
+8 -10
View File
@@ -3,7 +3,6 @@ from typing import Any, Optional
import orjson import orjson
from fastapi import Request, Response from fastapi import Request, Response
from fastapi.responses import JSONResponse
from comet.core.models import settings from comet.core.models import settings
@@ -116,7 +115,7 @@ def check_etag_match(request: Request, etag: str):
return False return False
class CachedJSONResponse(JSONResponse): class CachedJSONResponse(Response):
def __init__( def __init__(
self, self,
content: Any, content: Any,
@@ -127,18 +126,17 @@ class CachedJSONResponse(JSONResponse):
**kwargs, **kwargs,
): ):
body = orjson.dumps(content) body = orjson.dumps(content)
super().__init__(
super().__init__(content=content, status_code=status_code, **kwargs) content=body,
status_code=status_code,
self.body = body media_type="application/json",
**kwargs,
)
if cache_control: if cache_control:
self.headers["Cache-Control"] = cache_control.build() self.headers["Cache-Control"] = cache_control.build()
if etag: self.headers["ETag"] = etag or generate_etag(body)
self.headers["ETag"] = etag
else:
self.headers["ETag"] = generate_etag(body)
if vary: if vary:
self.headers["Vary"] = ", ".join(vary) self.headers["Vary"] = ", ".join(vary)