Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/opal/api/routes/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,17 @@ def update_requirement(
old_values = get_model_dict(req)
# Nullable fields clear on explicit null (model_fields_set distinguishes
# absent from null — issue #30); non-nullable fields keep the None-skip.
clearable = {"rationale", "category", "verification_method", "tbr_owner_id", "tbr_due"}
# parent_id clears too: a level-0 root has no parent, so promoting a child
# back to a root is a legitimate edit. level is a separate field and is not
# adjusted here — the caller sets it in the same PATCH if it should change.
clearable = {
"rationale",
"category",
"verification_method",
"tbr_owner_id",
"tbr_due",
"parent_id",
}
for field in (
"title",
"statement",
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/test_se_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,3 +753,24 @@ def test_api_patch_explicit_null_clears_nullable_fields(client, test_user):
assert body["tbr_due"] is None
assert body["category"] == "propulsion"
assert body["title"] == req["title"] # non-nullable untouched


def test_api_patch_explicit_null_clears_parent(client):
"""Issue #30 — parent_id is nullable ('level-0 roots have no parent'), so a
child promotes back to a root on explicit null. level is independent and is
only changed when the caller sends it."""
parent = _api_create(client, title="Vehicle", level=0)
child = _api_create(client, title="Engine", level=1, parent_id=parent["id"])
assert child["parent_id"] == parent["id"]

r = client.patch(f"/api/requirements/{child['id']}", json={"parent_id": None})
assert r.status_code == 200, r.text
body = r.json()
assert body["parent_id"] is None
assert body["level"] == 1 # untouched — clearing the parent does not relevel

# It stays cleared on reload, and omitting the field leaves it alone.
r = client.patch(f"/api/requirements/{child['id']}", json={"title": "Engine A"})
assert r.status_code == 200, r.text
assert r.json()["parent_id"] is None
assert client.get(f"/api/requirements/{child['id']}").json()["parent_id"] is None