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

fix openapi fields rendering for range type filters #1323

Closed
Show file tree
Hide file tree
Changes from 6 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
38 changes: 30 additions & 8 deletions django_filters/rest_framework/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,29 @@ def get_coreschema_field(self, field):
description=str(field.extra.get('help_text', ''))
)

def build_coreapi_field(self, name, field):
return compat.coreapi.Field(
name=name,
required=field.extra['required'],
location='query',
schema=self.get_coreschema_field(field),
)

def get_schema_field_names(self, field_name, field):
"""
Get the corresponding schema field names required to generate the openAPI schema
by referencing the widget suffixes if available.
"""
try:
suffixes = field.field_class.widget.suffixes
except AttributeError:
return [field_name]
else:
return [field_name] if not suffixes else [
'{}_{}'.format(field_name, suffix)
for suffix in suffixes if suffix
]

def get_schema_fields(self, view):
# This is not compatible with widgets where the query param differs from the
# filter's attribute name. Notably, this includes `MultiWidget`, where query
Expand All @@ -130,14 +153,13 @@ def get_schema_fields(self, view):

filterset_class = self.get_filterset_class(view, queryset)

return [] if not filterset_class else [
compat.coreapi.Field(
name=field_name,
required=field.extra['required'],
location='query',
schema=self.get_coreschema_field(field)
) for field_name, field in filterset_class.base_filters.items()
]
if not filterset_class:
return []

return [self.build_coreapi_field(schema_field_name, field)
for field_name, field in filterset_class.base_filters.items()
for schema_field_name in self.get_schema_field_names(field_name, field)
]
A-Ashiq marked this conversation as resolved.
Show resolved Hide resolved

def get_schema_operation_parameters(self, view):
A-Ashiq marked this conversation as resolved.
Show resolved Hide resolved
try:
Expand Down
40 changes: 40 additions & 0 deletions tests/rest_framework/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,46 @@ class View(FilterClassRootView):

self.assertEqual(fields, ['text', 'decimal', 'date', 'f'])

def test_fields_with_range_type_filter_default_suffixes(self):

class F(SeveralFieldsFilter):
date = filters.DateFromToRangeFilter()

class View(FilterClassRootView):
filterset_class = F

view = View()
backend = DjangoFilterBackend()
fields = backend.get_schema_fields(view)
field_names = [f.name for f in fields]

self.assertIn("date_after", field_names)
self.assertIn("date_before", field_names)

def test_fields_with_range_type_filter_with_custom_suffixes(self):
custom_suffixes = ['previous', 'later']

mock_widget = mock.Mock(suffixes=custom_suffixes)
mock_field_class = mock.Mock(widget=mock_widget)

class CustomDateFromtoRangeFilter(filters.DateFromToRangeFilter):
field_class = mock_field_class

class F(SeveralFieldsFilter):
date = CustomDateFromtoRangeFilter()

class View(FilterClassRootView):
filterset_class = F

view = View()

backend = DjangoFilterBackend()
fields = backend.get_schema_fields(view)
field_names = [f.name for f in fields]

for custom_suffix in custom_suffixes:
self.assertIn('date_{}'.format(custom_suffix), field_names)


class GetSchemaOperationParametersTests(TestCase):
def test_get_operation_parameters_with_filterset_fields_list(self):
Expand Down