Skip to content

Commit

Permalink
Merge branch 'pr891'
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Dec 9, 2022
2 parents ea48b27 + 20a4c4e commit 0f01020
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
4 changes: 2 additions & 2 deletions drf_spectacular/templates/drf_spectacular/redoc.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
<body>
{% if settings %}
<div id="redoc-container"></div>
<script src="{{ dist }}/bundles/redoc.standalone.js"></script>
<script src="{{ redoc_standalone }}"></script>
<script>
const redocSettings = {{ settings|safe }};
Redoc.init("{{ schema_url }}", redocSettings, document.getElementById('redoc-container'))
</script>
{% else %}
<redoc spec-url="{{ schema_url }}"></redoc>
<script src="{{ dist }}/bundles/redoc.standalone.js"></script>
<script src="{{ redoc_standalone }}"></script>
{% endif %}
</body>
</html>
6 changes: 3 additions & 3 deletions drf_spectacular/templates/drf_spectacular/swagger_ui.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
{% if favicon_href %}<link rel="icon" href="{{ favicon_href }}">{% endif %}
<link rel="stylesheet" href="{{ dist }}/swagger-ui.css">
<link rel="stylesheet" href="{{ swagger_ui_css }}">
<style>
html { box-sizing: border-box; overflow-y: scroll; }
*, *:after, *:before { box-sizing: inherit; }
Expand All @@ -14,8 +14,8 @@
</head>
<body>
<div id="swagger-ui"></div>
<script src="{{ dist }}/swagger-ui-bundle.js"></script>
<script src="{{ dist }}/swagger-ui-standalone-preset.js"></script>
<script src="{{ swagger_ui_bundle }}"></script>
<script src="{{ swagger_ui_standalone }}"></script>
{% if script_url %}
<script src="{{ script_url }}"></script>
{% else %}
Expand Down
35 changes: 21 additions & 14 deletions drf_spectacular/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ class SpectacularJSONAPIView(SpectacularAPIView):
renderer_classes = [OpenApiJsonRenderer, OpenApiJsonRenderer2]


def _get_sidecar_url(package):
return static(f'drf_spectacular_sidecar/{package}')
def _get_sidecar_url(filepath):
return static(f'drf_spectacular_sidecar/{filepath}')


class SpectacularSwaggerView(APIView):
Expand All @@ -133,7 +133,9 @@ def get(self, request, *args, **kwargs):
return Response(
data={
'title': self.title,
'dist': self._swagger_ui_dist(),
'swagger_ui_css': self._swagger_ui_resource('swagger-ui.css'),
'swagger_ui_bundle': self._swagger_ui_resource('swagger-ui-bundle.js'),
'swagger_ui_standalone': self._swagger_ui_resource('swagger-ui-standalone-preset.js'),
'favicon_href': self._swagger_ui_favicon(),
'schema_url': self._get_schema_url(request),
'settings': self._dump(spectacular_settings.SWAGGER_UI_SETTINGS),
Expand Down Expand Up @@ -172,14 +174,16 @@ def _get_schema_auth_names(self):
]
return [auth.name for auth in auth_extensions if auth]

def _swagger_ui_dist(self):
@staticmethod
def _swagger_ui_resource(filename):
if spectacular_settings.SWAGGER_UI_DIST == 'SIDECAR':
return _get_sidecar_url('swagger-ui-dist')
return spectacular_settings.SWAGGER_UI_DIST
return _get_sidecar_url(f'swagger-ui-dist/{filename}')
return f'{spectacular_settings.SWAGGER_UI_DIST}/{filename}'

def _swagger_ui_favicon(self):
@staticmethod
def _swagger_ui_favicon():
if spectacular_settings.SWAGGER_UI_FAVICON_HREF == 'SIDECAR':
return _get_sidecar_url('swagger-ui-dist') + '/favicon-32x32.png'
return _get_sidecar_url('swagger-ui-dist/favicon-32x32.png')
return spectacular_settings.SWAGGER_UI_FAVICON_HREF


Expand Down Expand Up @@ -209,7 +213,9 @@ def get(self, request, *args, **kwargs):
return Response(
data={
'title': self.title,
'dist': self._swagger_ui_dist(),
'swagger_ui_css': self._swagger_ui_resource('swagger-ui.css'),
'swagger_ui_bundle': self._swagger_ui_resource('swagger-ui-bundle.js'),
'swagger_ui_standalone': self._swagger_ui_resource('swagger-ui-standalone-preset.js'),
'favicon_href': self._swagger_ui_favicon(),
'script_url': set_query_parameters(
url=script_url,
Expand All @@ -235,7 +241,7 @@ def get(self, request, *args, **kwargs):
return Response(
data={
'title': self.title,
'dist': self._redoc_dist(),
'redoc_standalone': self._redoc_standalone(),
'schema_url': self._get_schema_url(request),
'settings': self._dump(spectacular_settings.REDOC_UI_SETTINGS),
},
Expand All @@ -250,10 +256,11 @@ def _dump(self, data):
else:
return json.dumps(data, indent=2)

def _redoc_dist(self):
@staticmethod
def _redoc_standalone():
if spectacular_settings.REDOC_DIST == 'SIDECAR':
return _get_sidecar_url('redoc')
return spectacular_settings.REDOC_DIST
return _get_sidecar_url('redoc/bundles/redoc.standalone.js')
return f'{spectacular_settings.REDOC_DIST}/bundles/redoc.standalone.js'

def _get_schema_url(self, request):
schema_url = self.url or get_relative_url(reverse(self.url_name, request=request))
Expand All @@ -273,4 +280,4 @@ class SpectacularSwaggerOauthRedirectView(RedirectView):
``SPECTACULAR_SETTINGS.SWAGGER_UI_SETTINGS.oauth2RedirectUrl`` django settings.
"""
def get_redirect_url(self, *args, **kwargs):
return _get_sidecar_url("swagger-ui-dist") + "/oauth2-redirect.html?" + self.request.GET.urlencode()
return _get_sidecar_url("swagger-ui-dist/oauth2-redirect.html") + "?" + self.request.GET.urlencode()

0 comments on commit 0f01020

Please sign in to comment.