Fix ViewInspector sharing view state across views (#6877) - #10031
Open
abdurrahimcs50 wants to merge 2 commits into
Open
Fix ViewInspector sharing view state across views (#6877)#10031abdurrahimcs50 wants to merge 2 commits into
abdurrahimcs50 wants to merge 2 commits into
Conversation
A single ViewInspector instance (e.g. an AutoSchema assigned via @action(schema=...)) can end up attached to multiple views when the action is defined on a mixin reused by several ViewSets. Since `view` was stored as a plain mutable attribute on the shared instance, each view that touched it would overwrite `.view` for every other view sharing it, causing schema generation to collapse onto whichever view set it last. __set__ now copies the schema instance per view before binding `view` to it, so each view gets its own independent copy instead of sharing mutable state.
The base tox environment doesn't install the optional uritemplate dependency, and SchemaGenerator.get_schema() asserts on it while computing path parameters. Guard the new test the same way the rest of tests/schemas/test_openapi.py already does.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes #6877.
A single
ViewInspectorinstance (e.g. anAutoSchemaassigned via@action(schema=AutoSchema())) can end up attached to multiple views when the action is defined on a mixin that's reused across severalViewSets, since the decorator only evaluatesAutoSchema()once, at decoration time.ViewInspector.__set__storedviewas a plain mutable attribute directly on that shared instance, so each view that touched it during schema generation would overwrite.viewfor every other view sharing the same instance. The result: schema generation collapses onto whichever view happened to set it last, producing duplicate/incorrectoperationIds, descriptions, and request/response schemas for the other views.Reproduction
Before this fix, generating the schema for both viewsets produces the same
operationIdfor bothcancelactions (and DRF itself warns about the resulting duplicate operationId).Fix
__set__now makes a shallowcopy.copy()of the schema instance before binding.viewto it, so each view gets its own independent copy instead of mutating shared state.Testing
tests/schemas/test_view_inspector.py, reproducing the exact mixin scenario from Custom action in a mixin class with custom schema results in incorrect schema generation聽#6877 and asserting each view gets a distinctoperationId../runtests.py).pre-commitpasses.