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

35 changes: 27 additions & 8 deletions django_filters/rest_framework/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,26 @@ 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 [f'{field_name}_{suffix}' for suffix in suffixes if suffix]
A-Ashiq marked this conversation as resolved.
Show resolved Hide resolved

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 +150,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