Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SchemaGenerator does not pass in serializer context #4382

Closed
kevin-brown opened this issue Aug 10, 2016 · 0 comments
Closed

SchemaGenerator does not pass in serializer context #4382

kevin-brown opened this issue Aug 10, 2016 · 0 comments
Labels
Milestone

Comments

@kevin-brown
Copy link
Member

kevin-brown commented Aug 10, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants