Repository navigation
Fix ViewInspector sharing view state across views (#6877) - #10031
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.
Pavel-Shcherbo
left a comment
There was a problem hiding this comment.
Independently checked head 5986e033036e against base 5668281ee6a9 on Python 3.14.6 / Django 6.0.10 (macOS arm64).
The new mixin regression plus test_openapi.py pass: 60 tests and 18 subtests. I also ran three descriptor-level controls for independent view bindings, leaving the template unbound, and None/reassignment behavior; all pass on head. With the exact base inspectors.py, the PR regression and all three additional controls fail (59 existing tests and 18 subtests still pass).
One compact regression worth retaining alongside the schema-generation case checks the binding directly:
def test_shared_schema_keeps_both_view_bindings():
template = AutoSchema(tags=["shared"], operation_id_base="Example")
first, second = APIView(), APIView()
first.schema = template
second.schema = template
assert first.schema is not second.schema
assert first.schema.view is first
assert second.schema.view is second
assert first.schema.get_tags("/example/", "GET") == ["shared"]
assert second.schema.operation_id_base == "Example"This needs APIView from rest_framework.views. The shallow copy isolates the view binding; this check does not assert deep isolation of mutable schema configuration. I found no blocker in these focused checks. I have not run the full test suite or Django/Python compatibility matrix.
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.