Skip to content

Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring

fix(inventory): stop a partial node update from blanking the other list

Pouzor71e29362aa27
diff --git a/backend/app/services/inventory_sync.py b/backend/app/services/inventory_sync.py
index 67875a2..165529a 100644
--- a/backend/app/services/inventory_sync.py
+++ b/backend/app/services/inventory_sync.py
@@ -195,7 +195,9 @@ def merge_facts_into_device(
* ``replace_lists`` — properties/services are taken wholesale rather than
unioned. True only for a user's save: otherwise a property they deleted
would come straight back on the next one. The backfill unions, so nothing
- any canvas recorded is lost.
+ any canvas recorded is lost. It applies list by list: a list absent from
+ ``facts`` is left alone even in replace mode, so a partial update never
+ clears the one it did not send.
"""
for field in (*DEVICE_SCALARS, "label", "type"):
incoming = facts.get(field)
@@ -210,11 +212,15 @@ def merge_facts_into_device(
if facts.get("show_hardware") and not device.show_hardware:
device.show_hardware = True
- if replace_lists:
- device.properties = list(facts.get("properties") or [])
- device.services = list(facts.get("services") or [])
+ # Per list, and only for one the caller actually sent: a partial update
+ # carrying properties alone must leave services as they were, not blank them.
+ if replace_lists and "properties" in facts:
+ device.properties = list(facts["properties"] or [])
else:
device.properties = merge_properties(device.properties, facts.get("properties"))
+ if replace_lists and "services" in facts:
+ device.services = list(facts["services"] or [])
+ else:
device.services = merge_services(device.services, facts.get("services"))
# Live status: keep the freshest observation rather than the last writer.
diff --git a/backend/tests/test_inventory_sync.py b/backend/tests/test_inventory_sync.py
index 552aa49..ac3a61b 100644
--- a/backend/tests/test_inventory_sync.py
+++ b/backend/tests/test_inventory_sync.py
@@ -342,6 +342,74 @@ class TestRoutesKeepTheLinkInStep:
assert device.hostname == "nas2.lan"
assert device.notes == "moved to the garage"
+ @pytest.mark.asyncio
+ async def test_patching_one_list_leaves_the_other_alone(self, client: AsyncClient, headers, db_session):
+ """Replace applies list by list — an unsent list is not blanked."""
+ db_session.add(
+ InventoryDevice(
+ id="d-1",
+ ip="10.0.0.5",
+ services=[{"port": 22, "protocol": "tcp", "service_name": "ssh"}],
+ properties=[{"key": "Rack", "value": "A1", "icon": None, "visible": True}],
+ )
+ )
+ await db_session.commit()
+ node_id = (
+ await client.post(
+ "/api/v1/nodes",
+ json={"type": "nas", "label": "NAS", "ip": "10.0.0.5", "force": True},
+ headers=headers,
+ )
+ ).json()["id"]
+
+ # properties only: services must survive untouched.
+ res = await client.patch(
+ f"/api/v1/nodes/{node_id}",
+ json={"properties": [{"key": "Owner", "value": "me", "icon": None, "visible": True}]},
+ headers=headers,
+ )
+ assert res.status_code == 200
+ device = await db_session.get(InventoryDevice, "d-1")
+ await db_session.refresh(device)
+ assert [p["key"] for p in device.properties] == ["Owner"]
+ assert [s["port"] for s in device.services] == [22]
+
+ # services only: the properties just written must survive in turn.
+ res = await client.patch(
+ f"/api/v1/nodes/{node_id}",
+ json={"services": [{"port": 443, "protocol": "tcp", "service_name": "https"}]},
+ headers=headers,
+ )
+ assert res.status_code == 200
+ await db_session.refresh(device)
+ assert [s["port"] for s in device.services] == [443]
+ assert [p["key"] for p in device.properties] == ["Owner"]
+
+ @pytest.mark.asyncio
+ async def test_patching_a_list_empty_still_clears_it(self, client: AsyncClient, headers, db_session):
+ """An explicit empty list is a deletion, not an omission."""
+ db_session.add(
+ InventoryDevice(
+ id="d-1",
+ ip="10.0.0.5",
+ properties=[{"key": "Rack", "value": "A1", "icon": None, "visible": True}],
+ )
+ )
+ await db_session.commit()
+ node_id = (
+ await client.post(
+ "/api/v1/nodes",
+ json={"type": "nas", "label": "NAS", "ip": "10.0.0.5", "force": True},
+ headers=headers,
+ )
+ ).json()["id"]
+
+ res = await client.patch(f"/api/v1/nodes/{node_id}", json={"properties": []}, headers=headers)
+ assert res.status_code == 200
+ device = await db_session.get(InventoryDevice, "d-1")
+ await db_session.refresh(device)
+ assert device.properties == []
+
@pytest.mark.asyncio
async def test_the_second_canvas_sees_the_first_canvas_edit(self, client: AsyncClient, headers, db_session):
"""No per-node overrides: one device, one set of facts, every canvas."""