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

Fix LoginView and RegisterView response schema generation #283

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

philipkimmey
Copy link

@philipkimmey philipkimmey commented Jul 18, 2021

Hello! Due to use of serializers.SerializerMethodField() the generated OpenAPI schema wasn't previously accurate.

The schema will vary based on some project settings values, but with the default values the /login/ endpoint previously looked like:

  /login/:
    post:
      operationId: createLogin
      description: 'Check the credentials and return the REST Token

        if the credentials are valid and authenticated.

        Calls Django Auth login method to register User ID

        in Django session framework


        Accept the following POST parameters: username, password

        Return the REST Framework Token Object''s key.'
      parameters: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Login'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/Login'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/Login'
      responses:
        '201':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Login'
          description: ''
      tags:
      - login

(Which of course isn't the correct response payload)

And now looks like:

  /login/:
    post:
      operationId: createLogin
      description: 'Check the credentials and return the REST Token

        if the credentials are valid and authenticated.

        Calls Django Auth login method to register User ID

        in Django session framework


        Accept the following POST parameters: username, password

        Return the REST Framework Token Object''s key.'
      parameters: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Login'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/Login'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/Login'
      responses:
        '201':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Token'
          description: ''
      tags:
      - login

@philipkimmey philipkimmey changed the title Fix LoginView response schema generation Fix LoginView and RegisterView response schema generation Jul 18, 2021
super().__init__(*args, **kwargs)


class DynamicFieldSerializerMetaclass(serializers.SerializerMetaclass):
Copy link
Author

Choose a reason for hiding this comment

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

TODO: Add tests

@@ -189,30 +191,46 @@ class Meta:
read_only_fields = ('email',)


class JWTSerializer(serializers.Serializer):
class DynamicSerializerField(Field):
Copy link
Author

Choose a reason for hiding this comment

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

TODO: Add tests


user_data = JWTUserDetailsSerializer(obj['user'], context=self.context).data
return user_data
user = DynamicSerializerField(
Copy link
Author

Choose a reason for hiding this comment

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

TODO: Add tests

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

Successfully merging this pull request may close these issues.

1 participant