Skip to content

Commit 426d996

Browse files
Fix error that prevents posting to api/contents endpoint with no body (#937)
1 parent f373286 commit 426d996

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

jupyter_server/services/contents/handlers.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -208,28 +208,23 @@ async def post(self, path=""):
208208
raise web.HTTPError(400, "Cannot POST to files, use PUT instead.")
209209

210210
model = self.get_json_body()
211-
copy_from = model.get("copy_from")
212-
if (
213-
copy_from
214-
and (
215-
await ensure_async(cm.is_hidden(path))
216-
or await ensure_async(cm.is_hidden(copy_from))
217-
)
218-
and not cm.allow_hidden
219-
):
220-
raise web.HTTPError(400, f"Cannot copy file or directory {path!r}")
221-
222-
if model is not None:
211+
if model:
223212
copy_from = model.get("copy_from")
213+
if copy_from:
214+
if not cm.allow_hidden and (
215+
await ensure_async(cm.is_hidden(path))
216+
or await ensure_async(cm.is_hidden(copy_from))
217+
):
218+
raise web.HTTPError(400, f"Cannot copy file or directory {path!r}")
219+
else:
220+
await self._copy(copy_from, path)
221+
224222
ext = model.get("ext", "")
225223
type = model.get("type", "")
226224
if type not in {None, "", "directory", "file", "notebook"}:
227225
# fall back to file if unknown type
228226
type = "file"
229-
if copy_from:
230-
await self._copy(copy_from, path)
231-
else:
232-
await self._new_untitled(path, type=type, ext=ext)
227+
await self._new_untitled(path, type=type, ext=ext)
233228
else:
234229
await self._new_untitled(path)
235230

tests/services/contents/test_api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ async def test_create_untitled(jp_fetch, contents, contents_dir, _check_created)
349349
r = await jp_fetch("api", "contents", path, method="POST", body=json.dumps({"ext": ".ipynb"}))
350350
_check_created(r, str(contents_dir), path, name, type="notebook")
351351

352+
name = "untitled"
353+
r = await jp_fetch("api", "contents", path, method="POST", allow_nonstandard_methods=True)
354+
_check_created(r, str(contents_dir), path, name=name, type="file")
355+
352356

353357
async def test_create_untitled_txt(jp_fetch, contents, contents_dir, _check_created):
354358
name = "untitled.txt"

0 commit comments

Comments
 (0)