Skip to content

WIP: initial openapi schema generator implementation #669

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

Closed
wants to merge 19 commits into from
Closed
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ This release is not backwards compatible. For easy migration best upgrade first
* Avoid printing invalid pointer when api returns 404


### Added

* Add support for DJANGO REST Framework 3.10.

## [2.8.0] - 2019-06-13

This is the last release supporting Python 2.7, Python 3.4, Django Filter 1.1, Django REST Framework <=3.8 and Django 2.0.
Expand Down
2 changes: 2 additions & 0 deletions example/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ pyparsing
pytz
sqlparse
django-filter>=2.0
PyYAML>=5.1.1
coreapi>=2.3.3
8 changes: 8 additions & 0 deletions example/settings/dev.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import os

from rest_framework import VERSION as DRFVERSION

drf_version = tuple(int(x) for x in DRFVERSION.split('.'))

SITE_ID = 1
DEBUG = True

Expand All @@ -21,6 +25,7 @@
'django.contrib.sites',
'django.contrib.sessions',
'django.contrib.auth',
'rest_framework_json_api',
'rest_framework',
'polymorphic',
'example',
Expand Down Expand Up @@ -99,3 +104,6 @@
),
'TEST_REQUEST_DEFAULT_FORMAT': 'vnd.api+json'
}

if drf_version >= (3, 10):
REST_FRAMEWORK['DEFAULT_SCHEMA_CLASS'] = 'rest_framework_json_api.schemas.openapi.AutoSchema'
857 changes: 857 additions & 0 deletions example/tests/test_openapi.py

Large diffs are not rendered by default.

76 changes: 76 additions & 0 deletions example/tests/unit/test_filter_schema_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import pytest
from rest_framework import VERSION as DRFVERSION
from rest_framework import filters as drf_filters

from rest_framework_json_api import filters as dja_filters
from rest_framework_json_api.django_filters import backends

from example.views import EntryViewSet


class DummyEntryViewSet(EntryViewSet):
filter_backends = (dja_filters.QueryParameterValidationFilter, dja_filters.OrderingFilter,
backends.DjangoFilterBackend, drf_filters.SearchFilter)
filterset_fields = {
'id': ('exact',),
'headline': ('exact',),
}

def __init__(self):
# dummy up self.request since PreloadIncludesMixin expects it to be defined
self.request = None


# get_schema_operation_parameters is only available in DRF >= 3.10
drf_version = tuple(int(x) for x in DRFVERSION.split('.'))
pytestmark = pytest.mark.skipif(drf_version < (3, 10), reason="requires DRF 3.10 or higher")


def test_filters_get_schema_params():
"""
test all my filters for `get_schema_operation_parameters()`
"""
# list of tuples: (filter, expected result)
filters = [
(dja_filters.QueryParameterValidationFilter, []),
(backends.DjangoFilterBackend, [
{
'name': 'filter[id]', 'required': False, 'in': 'query',
'description': 'id', 'schema': {'type': 'string'}
},
{
'name': 'filter[headline]', 'required': False, 'in': 'query',
'description': 'headline', 'schema': {'type': 'string'}
}
]),
(dja_filters.OrderingFilter, [
{
'name': 'sort', 'required': False, 'in': 'query',
'description': 'Which field to use when ordering the results.',
'schema': {'type': 'string'}
}
]),
(drf_filters.SearchFilter, [
{
'name': 'filter[search]', 'required': False, 'in': 'query',
'description': 'A search term.',
'schema': {'type': 'string'}
}
]),
]
view = DummyEntryViewSet()

for c, expected in filters:
f = c()
result = f.get_schema_operation_parameters(view)
assert len(result) == len(expected)
if len(result) == 0:
return
# py35: the result list/dict ordering isn't guaranteed
for res_item in result:
assert 'name' in res_item
for exp_item in expected:
if res_item['name'] == exp_item['name']:
assert res_item == exp_item
return
assert False
2 changes: 2 additions & 0 deletions requirements-development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ recommonmark==0.6.0
Sphinx==2.1.2
sphinx_rtd_theme==0.4.3
twine==1.13.0
django-oauth-toolkit==1.2.0
oauthlib==2.1.0
7 changes: 7 additions & 0 deletions rest_framework_json_api/django_filters/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,10 @@ def get_filterset_kwargs(self, request, queryset, view):
'request': request,
'filter_keys': filter_keys,
}

def get_schema_operation_parameters(self, view):
# wrap query parameters in 'filter[{}]'.format(name)
result = super(DjangoFilterBackend, self).get_schema_operation_parameters(view)
for field in result:
field['name'] = 'filter[{}]'.format(field['name'])
return result
Empty file.
Empty file.
10 changes: 10 additions & 0 deletions rest_framework_json_api/management/commands/generateschema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from rest_framework.management.commands.generateschema import Command as DRFCommand

from rest_framework_json_api.schemas.openapi import SchemaGenerator


class Command(DRFCommand):
help = "Generates jsonapi.org schema for project."

def get_generator_class(self):
return SchemaGenerator
Empty file.
Loading