Fix viewset actions dict being mutated after first request#9853
Fix viewset actions dict being mutated after first request#9853mag123c wants to merge 4 commits intoencode:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where the original actions dictionary passed to ViewSet.as_view() was being mutated after the first request, specifically by adding a 'head' key when a 'get' action was present.
- Creates a copy of the actions dictionary before any modifications
- Updates all internal references to use the copied dictionary
- Adds a regression test to ensure the original dictionary remains unchanged
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| rest_framework/viewsets.py | Creates a copy of the actions dict (bound_actions) before modification and uses it for all internal operations, preventing mutation of the original dict |
| tests/test_viewsets.py | Adds a regression test to verify the original actions dict is not mutated after calling as_view() or making requests |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
I have examined this structure and the solution seems reasonable. It solves the mutation problem. |
| assert 'head' not in actions | ||
| view(factory.get('/')) | ||
| assert 'head' not in actions | ||
| assert view.actions == {'get': 'list'} |
There was a problem hiding this comment.
So head is now never present in the viewset action... Another way to solve this mutation issue would be to make sure it's always there...
I'm not sure which way is actually more desirable
There was a problem hiding this comment.
Both approaches work since self.action_map contains head either way. The only difference is whether view.actions reflects the original input or the effective mappings.
What's your thought on this?
Fix the original
actionsdict passed toViewSet.as_view()being mutated after the first request.refs #9747