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

500 error on invalid parameter when using QueryParameterValidationFilter with BrowsableAPIRenderer #665

Open
CDavantzis opened this issue Jun 17, 2019 · 1 comment
Labels

Comments

@CDavantzis
Copy link

CDavantzis commented Jun 17, 2019

If a viewset is using the filter backend rest_framework_json_api.filters.QueryParameterValidationFilter the server will throw a 500
error when an invalid parameter is provided and the response is being rendered using rest_framework.renderers.BrowsableAPIRenderer.

The below code snippet is one way to fix the issue.

import rest_framework.renderers
import rest_framework.exceptions

class BrowsableAPIRenderer(rest_framework.renderers.BrowsableAPIRenderer):

    def get_filter_form(self, *args, **kwargs):
        """
        override `get_filter_form` so that if a user uses
        uses a filter backend that throws a `ValidationError`
        such as `QueryParameterValidationFilter` the server
        will return the proper 4XX error with incorrect query
        parameters, rather than a 500 error.
        """
        try:
            return super(BrowsableAPIRenderer, self).get_filter_form(*args, **kwargs)
        except rest_framework.exceptions.ValidationError:
            return ""

It may be in the scope of this project to add this fix to
rest_framework_json_api.renderers or mention it in the documentation.

There are other issues with allowing the BrowsableApiRenderer with the
QueryParameterValidationFilter such as the 'format' option the browsable
api provides.

class QueryParameterValidationFilter(rest_framework_json_api.filters.QueryParameterValidationFilter):

    query_regex = re.compile(r'^(sort|include|format)$|^(filter|fields|page)(\[[\w\.\-]+\])?$')

Users can use the above fix to allow format, however they may want to do something more complex such as overriding QueryParameterValidationFilter.validate_query_params so that it uses a different
query_regex based on the if the view is a subclass of rest_framework.renderers.BrowsableAPIRenderer

@sliverc
Copy link
Member

sliverc commented Jun 21, 2019

As DJA we support BrowsableAPIRenderer so using the different filter backend should work. I haven't looked into it in detail so not sure whether you fix is the only solution. Feel free thought to work on a PR also with a reproducing test.

In terms of format parameter see discussion in #535

@sliverc sliverc changed the title Server throws 500 error on invalid parameter when using QueryParameterValidationFilter and BrowsableAPIRenderer together 500 error on invalid parameter when using QueryParameterValidationFilter with BrowsableAPIRenderer Jun 21, 2019
@sliverc sliverc added the bug label Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants