question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom GET "action" endpoint not displaying query params

See original GitHub issue

Describe the bug Query params don’t show up in an “@action” that doesn’t take a list serializer class.

I understand the point of view here of query params being for listing resources, but I’d like to know if there’s any way to bypass this check to load the query params anyway.

To Reproduce

This won’t display query params.

class UserViewSet(GenericViewSet, ListModelMixin):
    filterset_class = FilterSetClass

    @action(
        detail=False,
        methods=["GET"],
        # Using this serializer solely to display correct output
        serializer_class=NonListSerializer,
    )
    def export_csv(...):
        # Use query params to setup an async job
        return Response({"job_id": <uuid>})

Expected behavior Should display query params correctly.

After a quick look at the code, seems like if if is_list_serializer(serializer) and if hasattr(self.view, 'action') happen after if isinstance(self.view, ListModelMixin) at drf_spectacular.openapi.AutoSchema._is_list_view, then it’d render the query params alright.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jourdanrodriguescommented, Jun 23, 2021

Thanks for the clarification, @tfranzel! I finally managed to get it working. I actually made a mistake in that PR I sent. Here’s the snipper for historical purpose:

from drf_spectacular.extensions import OpenApiFilterExtension
from drf_spectacular.openapi import AutoSchema as SpectacularAutoSchema


class AutoSchema(SpectacularAutoSchema):
    def _get_filter_parameters(self):
        if not (self._is_a_general_list_view() or self._is_list_view()):
            return []
        if getattr(self.view, 'filter_backends', None) is None:
            return []

        parameters = []
        for filter_backend in self.view.filter_backends:
            filter_extension = OpenApiFilterExtension.get_match(filter_backend())
            if filter_extension:
                parameters += filter_extension.get_schema_operation_parameters(self)
            else:
                parameters += filter_backend().get_schema_operation_parameters(self.view)
        return parameters

    def _is_a_general_list_view(self):
        return hasattr(self.view, "detail") and self.method.lower() == "get" and not self.view.detail
1reaction
tfranzelcommented, Jun 23, 2021

https://github.com/tfranzel/drf-spectacular/blob/deb83250f078ed4bce17b12972e098409ac69ec9/drf_spectacular/openapi.py#L367

is where you would insert something that bypasses self._is_list_view() for your specific filterset class. i would not touch is_list_view itself but rather bypass that return [] only in your specific case emerges.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Query string not working while using attribute routing
Query strings are behaving the same way as before. Update 2 I now have a singular action in my controller: [RoutePrefix("1/Names")] public ...
Read more >
Tutorial: Build a REST API with HTTP non-proxy integration
Learn how to create an API Gateway API with the HTTP custom integration using the ... The preceding endpoint can take two query...
Read more >
URLSearchParams - Web APIs - MDN Web Docs
Chrome Edge URLSearchParams Full support. Chrome49. Toggle history Full support. Edge... @@iterator Full support. Chrome49. Toggle history Full support. Edge... URLSearchParams() constructor Full support. Chrome49. Toggle...
Read more >
ASP.NET Core Blazor routing and navigation - Microsoft Learn
Learn how to manage request routing in Blazor apps and how to use the Navigation Manager and NavLink component for navigation.
Read more >
Routers - Django REST framework
Typically you won't need to specify the basename argument, but if you have a viewset where you've defined a custom get_queryset method, then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found