Skip to content

Fix ViewInspector sharing view state across views (#6877) - #10031

Open
abdurrahimcs50 wants to merge 2 commits into
encode:mainfrom
abdurrahimcs50:fix/6877-shared-view-inspector-schema
Open

abdurrahimcs50 wants to merge 2 commits into
encode:mainfrom
abdurrahimcs50:fix/6877-shared-view-inspector-schema

Conversation

@abdurrahimcs50

Copy link
Copy Markdown

Problem

Fixes #6877.

A single ViewInspector instance (e.g. an AutoSchema assigned via @action(schema=AutoSchema())) can end up attached to multiple views when the action is defined on a mixin that's reused across several ViewSets, since the decorator only evaluates AutoSchema() once, at decoration time.

ViewInspector.__set__ stored view as a plain mutable attribute directly on that shared instance, so each view that touched it during schema generation would overwrite .view for every other view sharing the same instance. The result: schema generation collapses onto whichever view happened to set it last, producing duplicate/incorrect operationIds, descriptions, and request/response schemas for the other views.

Reproduction

class CancelViewSetMixin:
    @action(methods=['post'], detail=True, schema=AutoSchema())
    def cancel(self, request, pk):
        return Response()

class ViewSetA(CancelViewSetMixin, GenericViewSet):
    serializer_class = ASerializer

class ViewSetB(CancelViewSetMixin, GenericViewSet):
    serializer_class = BSerializer

Before this fix, generating the schema for both viewsets produces the same operationId for both cancel actions (and DRF itself warns about the resulting duplicate operationId).

Fix

__set__ now makes a shallow copy.copy() of the schema instance before binding .view to it, so each view gets its own independent copy instead of mutating shared state.

Testing

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.
@auvipy auvipy added this to the 3.19 milestone Oct 5, 2026

@Pavel-Shcherbo Pavel-Shcherbo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

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.

Custom action in a mixin class with custom schema results in incorrect schema generation

3 participants