fix: normalize null list fields to [] in update_data before DB write

When a client sends null for assigned_team_ids or assigned_key_ids, ensure
the DB receives [] instead of null, preventing null from being stored where
empty list is expected. Extend test to verify the DB call uses [].

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
yuneng-jiang
2026-02-26 20:53:30 -08:00
co-authored by Claude Sonnet 4.6
parent 2144e79bad
commit cde23e9b6e
2 changed files with 8 additions and 1 deletions
@@ -392,6 +392,8 @@ async def update_access_group(
update_fields = data.model_dump(exclude_unset=True)
update_data: dict = {"updated_by": user_api_key_dict.user_id}
for field, value in update_fields.items():
if field in ("assigned_team_ids", "assigned_key_ids") and value is None:
value = []
update_data[field] = value
# Initialize delta lists before the try block so they remain accessible
@@ -1169,7 +1169,7 @@ def test_delete_access_group_handles_out_of_sync_assigned_keys(client_and_mocks)
def test_update_access_group_null_assigned_ids_treated_as_empty(client_and_mocks):
"""Update with explicit null for assigned_*_ids clears the list without TypeError."""
"""Update with explicit null for assigned_*_ids clears the list and writes [] to DB."""
client, _, mock_table, *_ = client_and_mocks
existing = _make_access_group_record(
@@ -1185,3 +1185,8 @@ def test_update_access_group_null_assigned_ids_treated_as_empty(client_and_mocks
json={"assigned_team_ids": None, "assigned_key_ids": None},
)
assert resp.status_code == 200
# Verify the DB update was called with [] (not null) for list fields
update_call_kwargs = mock_table.update.call_args.kwargs
assert update_call_kwargs["data"]["assigned_team_ids"] == []
assert update_call_kwargs["data"]["assigned_key_ids"] == []