Skip to content

Commit

Permalink
Upgrade django-rest-swagger to 2.1.2 because support for Django 1.11
Browse files Browse the repository at this point in the history
- Simplify lookup_value_regex for viewsets on Interface, Circuit to be
  compatible with Swagger schema generation
- Added a new regression test for `/docs/` to make sure that Swagger
  schema generation works
- Added other underlying requirements for django-rest-swagger to work
  (including CoreAPI and OpenAPI codecs!)
  • Loading branch information
jathanism committed Mar 7, 2018
1 parent d528580 commit 62f5a49
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
5 changes: 3 additions & 2 deletions nsot/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ class NetworkViewSet(ResourceViewSet):
queryset = models.Network.objects.all()
serializer_class = serializers.NetworkSerializer
filter_class = filters.NetworkFilter
lookup_value_regex = '[a-fA-F0-9:.]+(?:\/\d+)?'
lookup_value_regex = '[a-fA-F0-9:./]+'
natural_key = 'cidr'

def allocate_networks(self, networks, site_pk, state='allocated'):
Expand Down Expand Up @@ -655,7 +655,7 @@ class InterfaceViewSet(ResourceViewSet):
filter_class = filters.InterfaceFilter
# Match on device_hostname:name or pk id
# Being pretty vague here, so as to be minimally prescriptive
lookup_value_regex = '[^:]+:([^/]+|.+[0-9])|[0-9]+'
lookup_value_regex = '[a-zA-Z0-9:./-]*[0-9]'
natural_key = 'name_slug'

@cache_response(cache_errors=False, key_func=cache.list_key_func)
Expand Down Expand Up @@ -950,6 +950,7 @@ def rotate_secret_key(self, request, pk=None, *args, **kwargs):

class NotFoundViewSet(viewsets.GenericViewSet):
"""Catchall for bad API endpoints."""
exclude_from_schema = True
permission_classes = (permissions.IsAuthenticated,)

def get_queryset(self):
Expand Down
8 changes: 6 additions & 2 deletions nsot/conf/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import RedirectView
from rest_framework_swagger.views import get_swagger_view

from ..api.views import NotFoundViewSet
from ..ui.views import FeView
Expand All @@ -15,6 +16,9 @@
handler404 = 'nsot.ui.views.handle404'
handler500 = 'nsot.ui.views.handle500'

# This is the basic API explorer for Swagger/OpenAPI 2.0
schema_view = get_swagger_view(title='NSoT API')


urlpatterns = [
# API
Expand All @@ -23,8 +27,8 @@
# Catchall for missing endpoints
url(r'^api/.*/$', NotFoundViewSet.as_view({'get': 'list'})),

# Docs (Swagger)
url(r'^docs/', include('rest_framework_swagger.urls')),
# Docs (Swagger 2.0)
url(r'^docs/', schema_view, name='swagger'),

# Admin
url(r'^admin/', include(admin.site.urls)),
Expand Down
11 changes: 9 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ pyasn1~=0.1.9
six~=1.10.0
pycparser~=2.14.0
backports.ssl-match-hostname~=3.4.0.2
idna~=2.0.0
idna~=2.6.0
coreapi~=2.3.3
coreschema~=0.0.4
cryptography~=1.5.0
certifi~=2018.1.18
dj-static~=0.0.6
Expand All @@ -15,7 +17,7 @@ django-filter~=1.1.0
django-guardian~=1.4.9
django-jinja~=2.4.1
django-macaddress~=1.4.1
django-rest-swagger~=0.3.5
django-rest-swagger~=2.1.2
djangorestframework~=3.7.7
djangorestframework-bulk~=0.2.1
drf-extensions~=0.3.1
Expand All @@ -25,10 +27,15 @@ gunicorn~=19.3.0
greenlet~=0.4.9
ipaddress~=1.0.14
ipython~=3.1.0
itypes~=1.1.0
Jinja2~=2.8.0
logan~=0.7.2
MarkupSafe~=0.23.0
netaddr~=0.7.18
openapi-codec~=1.3.2
PyYAML~=3.11.0
requests~=2.18.4
simplejson~=3.13.2
static3~=0.6.1
typing~=3.6.4
uritemplate~=3.0.0
7 changes: 7 additions & 0 deletions tests/api_tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,10 @@ def test_bogus_url_raises_404(client, site):
client.get(bogus_url),
status.HTTP_404_NOT_FOUND
)


def test_swagger_docs_work(client):
"""Make sure that Swagger endpoint returns 200."""
swagger_uri = reverse('swagger')
resp = client.get(swagger_uri)
assert resp.ok

0 comments on commit 62f5a49

Please sign in to comment.