Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Feature implementation: Deep request serializer & different serializers for specific response codes #69

Description

@dnsdev
  • DRF OpenAPI version: based on 1.0.0
  • Python version: *
  • Operating System: *

Description

I extended the drf_openapi Django app to support detailed request serializers for requests. Then I also needed different serializers for different status codes (e.g. 200 has a specific response and some error, maybe 503 has a completely different one).
This is already done, but I am not sure if this is compatible to the OpenAPI spec.
If you are interested in this feature, then I would fork this and create a pull request.

What I Did

Implemented the above functionality.
The drf_openapi module searches automatically for the correct serializer depending on the response code of the views' method.
Here are some screenshots of the extended usage:
image
image

Here is the django code for defining the views' serializers:

@view_config(request_serializer=UserLoginSerializer,
                 response_serializer={
                     200: (UserLoginResponseSerializer, 'Success'),
                     423: (UserLoginFailedResponseSerializer, 'Device not yet verified'),
                     400: (NoValidation, 'Bad Request - Invalid data given')
                 },
                 validate_response=True)
    def create(self, request, *args, **kwargs):
        """
        The user wants to authenticate against the backend and receives a token and his/her user ID on success.
        """
        ..... the views' code .....
        .....
        if something_went_wrong:
            return Response({'message': _('Verify device - email sent'), 'activation_id': device_activation_pin.pk},
                            status=status.HTTP_423_LOCKED)
        .....
        return Response({'message': _('logged in'), 'token': token.key, 'user_id': user.pk,
                         'created': token.created, 'device_id': device.pk},
                        status=status.HTTP_200_OK)

The "NoValidation" class passed as a serializer deactivates response validation for the specific response code.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions