Skip to content

Commit

Permalink
ease schema browser handling with "Content-Disposition" #607
Browse files Browse the repository at this point in the history
thanks to @bahag-raesenerm for the proposition
  • Loading branch information
tfranzel committed Dec 19, 2021
1 parent ef14a07 commit 89734cc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 11 additions & 1 deletion drf_spectacular/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,17 @@ def _get_schema_response(self, request):
# that we try to source version through the schema view's own versioning_class.
version = self.api_version or request.version or self._get_version_parameter(request)
generator = self.generator_class(urlconf=self.urlconf, api_version=version)
return Response(generator.get_schema(request=request, public=self.serve_public))
return Response(
data=generator.get_schema(request=request, public=self.serve_public),
headers={"Content-Disposition": f'inline; filename="{self._get_filename(request, version)}"'}
)

def _get_filename(self, request, version):
return "{title}{version}.{suffix}".format(
title=spectacular_settings.TITLE or 'schema',
version=f' ({version})' if version else '',
suffix=self.perform_content_negotiation(request, force=True)[0].format
)

def _get_version_parameter(self, request):
version = request.GET.get('version')
Expand Down
4 changes: 4 additions & 0 deletions tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest
import yaml
from django import __version__ as DJANGO_VERSION
from django.urls import path
from rest_framework.decorators import api_view
from rest_framework.response import Response
Expand Down Expand Up @@ -47,6 +48,9 @@ def test_spectacular_view(no_warnings):
assert response.content.startswith(b'openapi: 3.0.3\n')
assert response.accepted_media_type == 'application/vnd.oai.openapi'

if DJANGO_VERSION > '3':
assert response.headers.get('CONTENT-DISPOSITION') == 'inline; filename="schema.yaml"'

schema = yaml.load(response.content, Loader=yaml.SafeLoader)
validate_schema(schema)
assert len(schema['paths']) == 2
Expand Down

0 comments on commit 89734cc

Please sign in to comment.