Skip to content

Commit 77513d6

Browse files
committed
fix: Add defensive type checking to lifespan and admin seed functions
Discovered during Session 38: the redis_client.py type hint import fix revealed a deeper issue in the data model loading code. When iterating through layer JSON files to seed data, the code assumed all array items were dictionaries, but some items (especially in eva-model.json) are other types (strings, objects). Added type checks: - api/server.py lifespan: Filter out non-dict items before calling setdefault() - api/routers/admin.py seed endpoint: Same type safety improvements - Both now check isinstance(o, dict) before treating items as dicts This fixes: - tests/test_admin.py::test_T32_seed_loads_all_layers (was failing) - tests/test_admin.py::test_T36_row_version_increments_on_reseed (was failing) - All 9 admin tests now passing Root cause: When JSON has nested arrays or mixed content, dict.values() iteration would find the first list and assume it's all dicts. Now we explicitly filter to dicts-only to be defensive.
1 parent a81539d commit 77513d6

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

api/routers/admin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,11 @@ async def seed(
157157
objects = v
158158
break
159159

160+
# Ensure objects is a list and filter to dicts only
161+
if not isinstance(objects, list):
162+
objects = []
160163
# Normalise: ensure every object has an 'id' field
164+
objects = [o for o in objects if isinstance(o, dict)]
161165
for obj in objects:
162166
if "id" not in obj and "key" in obj:
163167
obj["id"] = obj["key"]

api/server.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ async def lifespan(app: FastAPI):
144144
if isinstance(v, list):
145145
objects = v
146146
break
147+
# Ensure objects is a list and filter to dicts only
148+
if not isinstance(objects, list):
149+
objects = []
147150
# normalise id + stamp source_file
151+
objects = [o for o in objects if isinstance(o, dict)]
148152
for obj in objects:
149153
if "id" not in obj and "key" in obj:
150154
obj["id"] = obj["key"]

0 commit comments

Comments
 (0)