Skip to content

refactor: update BatchDeleteInstance data handling in ChatUserSerializer - #8213

Merged
wxg0103 merged 1 commit into
v3from
pr@v3@fix_chat_user_batch_delete
Nov 30, 2026
Merged

wxg0103 merged 1 commit into
v3from
pr@v3@fix_chat_user_batch_delete

Conversation

@shaohuzhang1

Copy link
Copy Markdown
Contributor

refactor: update BatchDeleteInstance data handling in ChatUserSerializer --bug=1080671@tapd-45245355 --user=王孝刚 [AI接口测试] v3 批量删除对话用户接口恒 500(serializer 无 data 即调用 is_valid) https://www.tapd.cn/45245355/s/2178261

--bug=1080671@tapd-45245355 --user=王孝刚 [AI接口测试] v3 批量删除对话用户接口恒 500(serializer 无 data 即调用 is_valid) https://www.tapd.cn/45245355/s/2178261
return result.success(ChatUserSerializer.BatchDeleteInstance(data={"ids": request.data}).batch_delete())

class BatchAddGroup(APIView):
authentication_classes = [TokenAuth]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changed line is likely a correct DRF fix, but there is a more serious data-shape issue nearby.

1. data= keyword change

If ChatUserSerializer.BatchDeleteInstance is a DRF Serializer subclass, this change is correct:

BatchDeleteInstance(data={...})

In DRF, the first positional argument is instance, not data:

Serializer(instance=None, data=empty, **kwargs)

So the original:

BatchDeleteInstance({"ids": request.data})

would pass the dict as instance, not as input data, which is wrong for validation/serialization.

Caveat: If BatchDeleteInstance is a custom class, confirm its constructor actually accepts data=. If its signature is something like __init__(self, payload), the keyword change will break.

2. Main issue: request.data is likely nested incorrectly

This part is suspicious:

data={"ids": request.data}

If the client sends:

{"ids": [1, 2, 3]}

then request.data is:

{"ids": [1, 2, 3]}

and the serializer receives:

{"ids": {"ids": [1, 2, 3]}}

So ids becomes a dict instead of a list. That is almost certainly a bug.

It is also inconsistent with the operation-object lambda above:

get_operation_object=lambda r, k: get_batch_delete_user_operation_object(r.data.get("ids", [])),

which correctly extracts request.data.get("ids", []).

Suggested correction

def post(self, request: Request):
    ids = request.data.get("ids", [])
    serializer = ChatUserSerializer.BatchDeleteInstance(data={"ids": ids})

    if hasattr(serializer, "is_valid"):
        serializer.is_valid(raise_exception=True)

    return result.success(serializer.batch_delete())

If request.data can also be a raw list, handle that explicitly, but prefer a consistent payload shape:

{"ids": [1, 2, 3]}

3. Additional potential issues

  • Empty ids: Ensure ids=[] does not accidentally mean “delete all”. Reject empty lists.
  • Validation: Verify ids is a list of integers, not a string, dict, or arbitrary objects.
  • Batch size limit: Add a maximum number of IDs per request to avoid abuse or long-running transactions.
  • Authorization: Check that the requesting user is allowed to delete every ID, not just the first one.
  • Transactions: Wrap the batch delete in a transaction if partial deletion would be inconsistent.
  • Bulk operations: If batch_delete() loops and deletes one by one, optimize with filter(pk__in=ids).delete() or a bulk update/soft-delete.
  • Audit consistency: Use the same validated ids list for the operation object and the deletion logic.
  • QueryDict handling: If form-encoded requests are supported, request.data.get("ids") may not return a list. Use getlist("ids") or require JSON.

Verdict

The data= change is probably correct for DRF. However, the bigger issue is that {"ids": request.data} likely nests the whole request body under ids. Use request.data.get("ids", []) instead, and validate/authorize the IDs before deleting.

@wxg0103
wxg0103 merged commit 9124dfa into v3 Nov 30, 2026
3 of 4 checks passed
@wxg0103
wxg0103 deleted the pr@v3@fix_chat_user_batch_delete branch November 30, 2026 02:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants