Skip to content

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

Open

Description

@reupen

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

  1. Create a view set mixin with a custom action that specifies a custom schema and use it on two view sets.

    For example:

    class CancelViewSetMixin:
        # For simplicity, `AutoSchema` is used in this example, but this could be any schema class
        @action(methods=['post'], detail=True, schema=AutoSchema())
        def cancel(self, request, pk):
            return Response()
    
    
    class ViewSetA(CancelViewSetMixin, GenericViewSet):
        serializer_class = ModelASerializer
        queryset = ModelA.objects.all()
    
    
    class ViewSetB(CancelViewSetMixin, GenericViewSet):
        serializer_class = ModelBSerializer
        queryset = ModelB.objects.all()
  2. Register the routes e.g.

    router = DefaultRouter()
    router.register(r'view-set-a', ViewSetA)
    router.register(r'view-set-b', ViewSetB)
    
    
    urlpatterns = [
        path('', include(router.urls)),
    ]
  3. Run ./manage.py generateschema

Expected behavior

Different schemas generated for the cancel action in the two view sets.

Actual behavior

The same schema is generated for the cancel action in the two view sets (basically everything except the path are the same, so operationId, request schema, responses etc. are all shared).

Discussion

(There is some background to the general topic in #5992.)

This does not work as desired because same view instance ends up being used for schema generation for the two actions (but the correct path and method are used).

The fact that this doesn't work is probably not too shocking considering that view is an attribute of the schema instance, but it would be good if it did work.

It's also interesting to note that setting a schema on a base class using the schema attribute does work. There is a difference in when ViewInspector.__set__() is called in the two cases.

My first thought would be that it would be simpler to eliminate the state from schema classes and pass the view into the schema_class.get_operation() as an argument instead. Of course, there is a backwards compatibility consideration there.

Simply explicitly setting view on the schema instance before calling get_operation() also works (but perhaps a bit on the hacky side).

Activity

  1. carltongibson commented on Aug 20, 2019

    @carltongibson
    Collaborator

    Thanks for the report.

    This does not work as desired because same view instance ends up being used for schema generation for the two actions (but the correct path and method are used).

    This is the bug here. The view.schema triggers ViewDescriptor.__get__() which should set the view attribute on the returned instance.

    As such, this should be unnecessary:

    ...explicitly setting view on the schema instance before calling get_operation() also works ...

    The whole point of using the descriptor protocol here to do this automatically.

    So... if you have a cycle...

    First step is to extract the few test cases for this from tests.schemas.test_coreapi.TestAutoSchema (and maybe have a browse at those added in #5992) into a test_view_inspector module, just to assert the actual descriptor behaviour here.

    Then, add a test case using your mixin example to show the failure. Then we can correct that.

  2. stale commented on Oct 19, 2025

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions