Skip to content

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



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

@iMerica iMerica closed this Sep 13, 2025
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.

2 participants