Description
Steps to reproduce
Create a simple serializer which references the incoming serializer context. As an example, we're going to reference the incoming request, which should be passed in as request
, so we can get the user who is making the request to the serializer.
class MySimpleSerializer(Serializer):
# I have no fields because I'm a useless serializer
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
print(self.context['request'].user)
And attach it to a view which is pulled in for schema generation. It can be any view, as long as the schema generation is able to introspect it and generate a schema from it.
Expected behavior
The schema is able to be generated from the view. It should reference no fields (none are being defined) but it should still be able to generate an empty schema.
Actual behavior
During schema generation, an error is triggered because request
is not defined in self.context
. The serializer context is empty.
Possible solution
One possible solution to this issue is to generate the serializer context, which can be done using view.get_serializer_context
and pass it into the serializer that is being used for introspection. You can combine the steps for getting the serializer class, getting the context, and creating the serializer by using view.get_serializer
to create it.
The problem with that...
Right now that solution would run into another issue, which is that view.request
is not defined. This is required by view.get_serializer_context
, and the issue related to it is being tracked at #4278.