Skip to content

Commit

Permalink
implement get_schema_operation_parameters()
Browse files Browse the repository at this point in the history
  • Loading branch information
n2ygk committed May 31, 2019
1 parent 66bf756 commit 556ce27
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
21 changes: 20 additions & 1 deletion django_filters/rest_framework/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,23 @@ def get_schema_fields(self, view):
]

def get_schema_operation_parameters(self, view):
return []
try:
queryset = view.get_queryset()
except Exception:
queryset = None
warnings.warn(
"{} is not compatible with schema generation".format(view.__class__)
)

filterset_class = self.get_filterset_class(view, queryset)
return [] if not filterset_class else [
({
'name': field_name,
'required': field.extra['required'],
'in': 'query',
'description': field.label if field.label is not None else field_name,
'schema': {
'type': 'string',
},
}) for field_name, field in filterset_class.base_filters.items()
]
6 changes: 3 additions & 3 deletions docs/guide/rest_framework.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ You can override these methods on a case-by-case basis for each view, creating u
'author': self.get_author(),
}

Schema Generation with Core API
-------------------------------
Schema Generation with Core API and Open API
--------------------------------------------

The backend class integrates with DRF's schema generation by implementing ``get_schema_fields()``. This is automatically enabled when Core API is installed. Schema generation usually functions seamlessly, however the implementation does expect to invoke the view's ``get_queryset()`` method. There is a caveat in that views are artificially constructed during schema generation, so the ``args`` and ``kwargs`` attributes will be empty. If you depend on arguments parsed from the URL, you will need to handle their absence in ``get_queryset()``.
The backend class integrates with DRF's schema generation by implementing ``get_schema_fields()`` and ``get_schema_operation_parameters()``. ``get_schema_fields()`` is automatically enabled when Core API is installed. ``get_schema_operation_parameters()`` is always enabled for Open API (new since DRF 3.9). Schema generation usually functions seamlessly, however the implementation does expect to invoke the view's ``get_queryset()`` method. There is a caveat in that views are artificially constructed during schema generation, so the ``args`` and ``kwargs`` attributes will be empty. If you depend on arguments parsed from the URL, you will need to handle their absence in ``get_queryset()``.

For example, your get queryset method may look like this:

Expand Down
4 changes: 2 additions & 2 deletions tests/rest_framework/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ class GetSchemaOperationParametersTests(TestCase):
def test_get_operation_parameters_with_filterset_fields_list(self):
backend = DjangoFilterBackend()
fields = backend.get_schema_operation_parameters(FilterFieldsRootView())
fields = [f.name for f in fields]
fields = [f['name'] for f in fields]

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


class TemplateTests(TestCase):
Expand Down

0 comments on commit 556ce27

Please sign in to comment.