You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Custom action in a mixin class with custom schema results in incorrect schema generation #6877
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
Create a view set mixin with a custom action that specifies a custom schema and use it on two view sets.
For example:
classCancelViewSetMixin:
# For simplicity, `AutoSchema` is used in this example, but this could be any schema class@action(methods=['post'], detail=True, schema=AutoSchema())defcancel(self, request, pk):
returnResponse()
classViewSetA(CancelViewSetMixin, GenericViewSet):
serializer_class=ModelASerializerqueryset=ModelA.objects.all()
classViewSetB(CancelViewSetMixin, GenericViewSet):
serializer_class=ModelBSerializerqueryset=ModelB.objects.all()
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).
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.
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.
Activity
carltongibson commentedon Aug 20, 2019
Thanks for the report.
This is the bug here. The
view.schematriggersViewDescriptor.__get__()which should set theviewattribute on the returned instance.As such, this should be unnecessary:
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 atest_view_inspectormodule, 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.
stale commentedon Oct 19, 2025
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.