Skip to content

Adjust schema get_filter_fields rules to match framework #5454

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions rest_framework/schemas/inspectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,18 +337,33 @@ def get_pagination_fields(self, path, method):
paginator = view.pagination_class()
return paginator.get_schema_fields(view)

def get_filter_fields(self, path, method):
view = self.view
def _allows_filters(self, path, method):
"""
Determine whether to include filter Fields in schema.

if not is_list_view(path, method, view):
return []
Default implementation looks for ModelViewSet or GenericAPIView
actions/methods that cause filtering on the default implementation.

Override to adjust behaviour for your view.

if not getattr(view, 'filter_backends', None):
Note: Introduced in v3.7: Initially "private" (i.e. with leading underscore)
to allow changes based on user experience.
"""
if getattr(self.view, 'filter_backends', None) is None:
return False

if hasattr(self.view, 'action'):
return self.view.action in ["list", "retrieve", "update", "partial_update", "destroy"]

return method.lower in ["get", "put", "patch", "delete"]

def get_filter_fields(self, path, method):
if not self._allows_filters(path, method):
return []

fields = []
for filter_backend in view.filter_backends:
fields += filter_backend().get_schema_fields(view)
for filter_backend in self.view.filter_backends:
fields += filter_backend().get_schema_fields(self.view)
return fields

def get_encoding(self, path, method):
Expand Down
18 changes: 12 additions & 6 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ def test_anonymous_request(self):
url='/example/{id}/',
action='get',
fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String())
coreapi.Field('id', required=True, location='path', schema=coreschema.String()),
coreapi.Field('ordering', required=False, location='query', schema=coreschema.String(title='Ordering', description='Which field to use when ordering the results.'))
]
)
}
Expand Down Expand Up @@ -179,7 +180,8 @@ def test_authenticated_request(self):
url='/example/{id}/',
action='get',
fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String())
coreapi.Field('id', required=True, location='path', schema=coreschema.String()),
coreapi.Field('ordering', required=False, location='query', schema=coreschema.String(title='Ordering', description='Which field to use when ordering the results.'))
]
),
'custom_action': coreapi.Link(
Expand Down Expand Up @@ -225,7 +227,8 @@ def test_authenticated_request(self):
fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String()),
coreapi.Field('a', required=True, location='form', schema=coreschema.String(title='A', description=('A field description'))),
coreapi.Field('b', required=False, location='form', schema=coreschema.String(title='B'))
coreapi.Field('b', required=False, location='form', schema=coreschema.String(title='B')),
coreapi.Field('ordering', required=False, location='query', schema=coreschema.String(title='Ordering', description='Which field to use when ordering the results.'))
]
),
'partial_update': coreapi.Link(
Expand All @@ -235,14 +238,16 @@ def test_authenticated_request(self):
fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String()),
coreapi.Field('a', required=False, location='form', schema=coreschema.String(title='A', description='A field description')),
coreapi.Field('b', required=False, location='form', schema=coreschema.String(title='B'))
coreapi.Field('b', required=False, location='form', schema=coreschema.String(title='B')),
coreapi.Field('ordering', required=False, location='query', schema=coreschema.String(title='Ordering', description='Which field to use when ordering the results.'))
]
),
'delete': coreapi.Link(
url='/example/{id}/',
action='delete',
fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String())
coreapi.Field('id', required=True, location='path', schema=coreschema.String()),
coreapi.Field('ordering', required=False, location='query', schema=coreschema.String(title='Ordering', description='Which field to use when ordering the results.'))
]
)
}
Expand Down Expand Up @@ -450,7 +455,8 @@ def test_schema_for_regular_views(self):
url='/example1/{id}/',
action='get',
fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String())
coreapi.Field('id', required=True, location='path', schema=coreschema.String()),
coreapi.Field('ordering', required=False, location='query', schema=coreschema.String(title='Ordering', description='Which field to use when ordering the results.'))
]
)
}
Expand Down