Skip to content

Commit 17e6835

Browse files
nicoschmdtpatrickelectric
authored andcommitted
bag-of-holding: allow null as a valid payload value
1 parent 6042c01 commit 17e6835

File tree

1 file changed

+17
-2
lines changed
  • core/services/bag_of_holding

1 file changed

+17
-2
lines changed

core/services/bag_of_holding/main.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
from commonwealth.utils.apis import GenericErrorHandlingRoute
1111
from commonwealth.utils.logs import InterceptHandler, init_logger
1212
from commonwealth.utils.sentry_config import init_sentry_async
13-
from fastapi import Body, FastAPI, HTTPException
13+
from fastapi import Body, Depends, FastAPI, HTTPException
1414
from fastapi import Path as FastPath
15+
from fastapi import Request
1516
from fastapi.responses import HTMLResponse, JSONResponse
1617
from fastapi_versioning import VersionedFastAPI, version
1718
from loguru import logger
@@ -62,6 +63,17 @@ def write_db(data: Dict[str, Any]) -> None:
6263
json.dump(data, f)
6364

6465

66+
async def parse_nullable_body(request: Request) -> Any:
67+
body = await request.body()
68+
if not body:
69+
return None
70+
71+
try:
72+
return json.loads(body)
73+
except json.JSONDecodeError as e:
74+
raise HTTPException(status_code=400, detail=f"Invalid JSON: {e}") from e
75+
76+
6577
@app.post("/overwrite")
6678
async def overwrite_data(payload: dict[str, Any] = Body(...)) -> JSONResponse:
6779
logger.debug(f"Overwrite: {json.dumps(payload)}")
@@ -71,7 +83,10 @@ async def overwrite_data(payload: dict[str, Any] = Body(...)) -> JSONResponse:
7183

7284
@app.post("/set/{path:path}")
7385
@version(1, 0)
74-
async def write_data(path: str = FastPath(..., regex=r"^.*$"), payload: Any = Body(...)) -> JSONResponse:
86+
async def write_data(
87+
path: str = FastPath(..., pattern=r"^.*$"),
88+
payload: Any = Depends(parse_nullable_body),
89+
) -> JSONResponse:
7590
logger.debug(f"Write path: {path}, {json.dumps(payload)}")
7691
current_data = read_db()
7792
dpath.new(current_data, path, payload)

0 commit comments

Comments
 (0)