8000 Use serializers as a source for APIView schema parameters · Issue #7163 · encode/django-rest-framework · GitHub
[go: up one dir, main page]

Skip to content
Use serializers as a source for APIView schema parameters #7163
Closed
@domingues

Description

@domingues

I want to add filter like parameters to my APIView methods, they should appear in the schema and validate the incoming data, serializers are a perfect fit for this.

In this repo:
https://github.com/domingues/django-rest-framework/blob/0997748e4fbd0f330cfcac580b446d849e50f80e/rest_framework/schemas/openapi.py#L542-L586
we have a class APIViewSchema(AutoSchema) that provides (at the moment) two decorators: @APIViewSchema.serializer(serializer: serializers.Serializer) and @APIViewSchema.query_parameters(parameters: serializers.Serializer)
and use them like this:

class MySerializer(serializers.Serializer):
    count = serializers.IntegerField(help_text='Number of results.')


class MyView(views.APIView):
    schema = APIViewSchema()

    class Parameters(serializers.Serializer):
        q = serializers.CharField(required=True, min_length=3, help_text='Search by.')
        page_size = serializers.IntegerField(required=False, max_value=100, help_text='Number of results to return.')
        ordering = serializers.ChoiceField(choices=['title', 'rank'], default='rank',
                                           help_text='Order by.')

    @APIViewSchema.serializer(MySerializer())
    @APIViewSchema.query_parameters(Parameters())
    def get(self, request):
        parameters = self.Parameters(data=request.query_params)
        parameters.is_valid(raise_exception=True)
        data = ...
        return Response(MySerializer(data).data)

imagem

I think that would be really useful if we have something like this.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0