Skip to content

Commit

Permalink
Swagger documentation (cvat-ai#978)
Browse files Browse the repository at this point in the history
* Fix swagger problems (exceptions, /api/swagger.json, /api/docs/)
  • Loading branch information
nmanovic authored and Chris Lee-Messer committed Mar 5, 2020
1 parent 7333b31 commit e7826ea
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 12 deletions.
2 changes: 2 additions & 0 deletions cvat-ui/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

/node_modules
/dist
/build
/yarn.lock

19 changes: 13 additions & 6 deletions cvat/apps/engine/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ def to_internal_value(self, data):
return attribute

def to_representation(self, instance):
attribute = super().to_representation(instance)
attribute['values'] = attribute['values'].split('\n')
if instance:
attribute = super().to_representation(instance)
attribute['values'] = attribute['values'].split('\n')
else:
attribute = instance

return attribute

class LabelSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -75,8 +79,11 @@ def to_internal_value(self, data):

# pylint: disable=no-self-use
def to_representation(self, instance):
upload_dir = instance.task.get_upload_dirname()
return instance.file.path[len(upload_dir) + 1:]
if instance:
upload_dir = instance.task.get_upload_dirname()
return instance.file.path[len(upload_dir) + 1:]
else:
return instance

class ServerFileSerializer(serializers.ModelSerializer):
class Meta:
Expand All @@ -89,7 +96,7 @@ def to_internal_value(self, data):

# pylint: disable=no-self-use
def to_representation(self, instance):
return instance.file
return instance.file if instance else instance

class RemoteFileSerializer(serializers.ModelSerializer):
class Meta:
Expand All @@ -102,7 +109,7 @@ def to_internal_value(self, data):

# pylint: disable=no-self-use
def to_representation(self, instance):
return instance.file
return instance.file if instance else instance

class RqStatusSerializer(serializers.Serializer):
state = serializers.ChoiceField(choices=[
Expand Down
9 changes: 6 additions & 3 deletions cvat/apps/engine/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@
path('dashboard/', views.dispatch_request),

# documentation for API
path('api/swagger.<slug:format>$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
path('api/swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
path('api/docs/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
path('api/swagger<str:scheme>', views.wrap_swagger(
schema_view.without_ui(cache_timeout=0)), name='schema-json'),
path('api/swagger/', views.wrap_swagger(
schema_view.with_ui('swagger', cache_timeout=0)), name='schema-swagger-ui'),
path('api/docs/', views.wrap_swagger(
schema_view.with_ui('redoc', cache_timeout=0)), name='schema-redoc'),

# entry point for API
path('api/v1/auth/', include('cvat.apps.authentication.api_urls')),
Expand Down
16 changes: 16 additions & 0 deletions cvat/apps/engine/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@
from drf_yasg.inspectors import NotHandled, CoreAPICompatInspector
from django_filters.rest_framework import DjangoFilterBackend

# drf-yasg component doesn't handle correctly URL_FORMAT_OVERRIDE and
# send requests with ?format=openapi suffix instead of ?scheme=openapi.
# We map the required paramater explicitly and add it into query arguments
# on the server side.
def wrap_swagger(view):
@login_required
def _map_format_to_schema(request, scheme=None):
if 'format' in request.GET:
request.GET = request.GET.copy()
format_alias = settings.REST_FRAMEWORK['URL_FORMAT_OVERRIDE']
request.GET[format_alias] = request.GET['format']

return view(request, format=scheme)

return _map_format_to_schema

# Server REST API
@login_required
def dispatch_request(request):
Expand Down
4 changes: 2 additions & 2 deletions cvat/requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ GitPython==2.1.11
coreapi==2.3.3
django-filter==2.0.0
Markdown==3.0.1
djangorestframework==3.9.1
djangorestframework==3.9.3
Pygments==2.3.1
drf-yasg==1.16.0
drf-yasg==1.17.0
Shapely==1.6.4.post2
pdf2image==1.6.0
pascal_voc_writer==0.1.4
Expand Down
2 changes: 1 addition & 1 deletion cvat/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def generate_ssh_keys():
'rest_framework.filters.OrderingFilter'),

# Disable default handling of the 'format' query parameter by REST framework
'URL_FORMAT_OVERRIDE': None,
'URL_FORMAT_OVERRIDE': 'scheme',
}

REST_AUTH_REGISTER_SERIALIZERS = {
Expand Down

0 comments on commit e7826ea

Please sign in to comment.