Skip to content

Commit b9ac42e

Browse files
committed
Use drf-spectacular instead of drf-yasg for API docs
drf-yasg is not maintained and does not support OpenAPI 3.0. Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
1 parent f94d797 commit b9ac42e

4 files changed

Lines changed: 15 additions & 13 deletions

File tree

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Django==3.0.7
1313
django-filter==2.2.0
1414
djangorestframework==3.11.0
1515
django-widget-tweaks==1.4.8
16-
drf-yasg==1.17.1
16+
drf-spectacular==0.13.0
1717
gunicorn==19.7.1
1818
importlib-metadata==1.3.0
1919
ipython==7.13.0

vulnerabilities/templates/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<a class="navbar-item" href="{% url 'home' %}">
1515
Home
1616
</a>
17-
<a class="navbar-item" href="{% url 'schema-swagger-ui' %}">
17+
<a class="navbar-item" href="{% url 'swagger-ui' %}">
1818
API Docs
1919
</a>
2020
<div class="navbar-item has-dropdown is-hoverable">

vulnerablecode/settings.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
'rest_framework',
4949
'django_filters',
5050
'widget_tweaks',
51-
'drf_yasg',
51+
'drf_spectacular',
5252
]
5353

5454
MIDDLEWARE = [
@@ -159,8 +159,15 @@
159159
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
160160
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
161161
'PAGE_SIZE': 100,
162+
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
162163
}
163164

165+
SPECTACULAR_SETTINGS = {
166+
'SERVE_INCLUDE_SCHEMA': False,
167+
'TITLE': "VulnerableCode API"
168+
}
169+
# TODO: Specify the license for the API here.
170+
164171

165172
# Set `DJANGO_DEV=1` in env to enable dev mode
166173
if DEV_MODE:

vulnerablecode/urls.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323

2424
from django.contrib import admin
2525
from django.urls import include, path
26+
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
2627
from rest_framework import permissions
27-
from drf_yasg.views import get_schema_view
28-
from drf_yasg import openapi
2928
from rest_framework.routers import DefaultRouter
3029

30+
3131
from vulnerabilities.api import PackageViewSet
3232
from vulnerabilities.api import VulnerabilityViewSet
3333
from vulnerabilities.views import HomePage
@@ -41,12 +41,6 @@
4141
from vulnerabilities.views import VulnerabilityCreate
4242
from vulnerabilities.views import VulnerabilityReferenceCreate
4343

44-
schema_view = get_schema_view(
45-
openapi.Info(title="VulnerableCode API", default_version="v1"),
46-
public=True,
47-
permission_classes=(permissions.AllowAny,),
48-
)
49-
5044
api_router = DefaultRouter()
5145
api_router.register(r"packages", PackageViewSet)
5246
# `DefaultRouter` requires `basename` when registering viewsets which don't
@@ -56,6 +50,8 @@
5650

5751
urlpatterns = [
5852
path("admin/", admin.site.urls),
53+
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
54+
path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(), name='swagger-ui'),
5955
path("packages/search", PackageSearchView.as_view(), name="package_search"),
6056
path("packages/<int:pk>", PackageUpdate.as_view(), name="package_view"),
6157
path("vulnerabilities/<int:pk>", VulnerabilityDetails.as_view(), name="vulnerability_view"),
@@ -88,6 +84,5 @@
8884
name="vulnerability_reference_create",
8985
),
9086
path("", HomePage.as_view(), name="home"),
91-
path(r"api/", include(api_router.urls)),
92-
path(r"api/docs", schema_view.with_ui("swagger", cache_timeout=0), name="schema-swagger-ui"),
87+
path(r"api/", include(api_router.urls))
9388
]

0 commit comments

Comments
 (0)