Skip to content

Fix ViewSet action mapping mutation and initialize head in as_view - #10087

Open
RajGahoi wants to merge 1 commit into
encode:mainfrom
RajGahoi:fix/issue-9747-viewset-head-action-mutation
Open

RajGahoi wants to merge 1 commit into
encode:mainfrom
RajGahoi:fix/issue-9747-viewset-head-action-mutation

Conversation

@RajGahoi

Copy link
Copy Markdown

Fixes #9747

Description

In \ViewSetMixin.as_view(), the \�ctions\ dictionary passed into the method was captured by the inner \�iew(request, *args, **kwargs)\ closure without being copied. During request dispatch, the block:
\\python
if 'get' in actions and 'head' not in actions:
actions['head'] = actions['get']
\
mutated the \�ctions\ dictionary in-place. This caused two problems:

  1. The dictionary passed by the caller to \�s_view(actions)\ was mutated in-place.
  2. \�iew.actions\ mutated after the first request was processed, leading to non-deterministic state between pre-request and post-request inspection.

Solution

  • In \ViewSetMixin.as_view(), copy \�ctions\ via \�ctions = dict(actions)\ and populate the implicit 'head'\ mapping immediately at view creation time.
  • Remove the in-place mutation logic from within the \�iew()\ closure.
  • The caller's dictionary is preserved, and \�iew.actions\ is deterministic both before and after any request.

Testing

  • Added unit tests in \ ests/test_viewsets.py\ covering:
    • Preserving caller's \�ctions\ dictionary without mutation.
    • Ensuring \�iew.actions\ contains 'head'\ deterministically before and after requests.
    • Preserving explicitly configured \head\ actions.
    • Ensuring viewsets without \get\ do not have \head\ added.
  • Full test suite passed (1645 passed, 0 failed).
  • Flake8 checks pass with 0 errors.

…ions

Copy actions and map HEAD at as_view() creation time rather than
mutating the actions dictionary during request dispatch. This ensures the
caller's actions dictionary is not mutated in-place and view.actions is
deterministic both before and after requests are handled.

Fixes encode#9747

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

a viewset's action list gets "head" added to it, but only after being called at least once

1 participant