Skip to content

Commit

Permalink
Merge pull request #163 from Nix41/master
Browse files Browse the repository at this point in the history
Allow Swagger-UI configuration through settings Closes #162 and Closes #160
  • Loading branch information
tfranzel committed Oct 10, 2020
2 parents da66def + 8823bb0 commit 86dd8f7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions drf_spectacular/settings.py
Expand Up @@ -28,6 +28,10 @@
'SERVE_INCLUDE_SCHEMA': True,
'SERVE_PERMISSIONS': ['rest_framework.permissions.AllowAny'],

# Dictionary of configurations to pass to the SwaggerUI({ ... })
# https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/
'SWAGGER_UI_SETTINGS': {},

# Append OpenAPI objects to path and components in addition to the generated objects
'APPEND_PATHS': {},
'APPEND_COMPONENTS': {},
Expand Down
4 changes: 3 additions & 1 deletion drf_spectacular/templates/drf_spectacular/swagger_ui.html
Expand Up @@ -10,6 +10,7 @@
<div id="swagger-ui"></div>
<script src="//unpkg.com/swagger-ui-dist@3.35.0/swagger-ui-bundle.js"></script>
<script>
var swagger_settings = {{ settings|safe }}
const ui = SwaggerUIBundle({
url: "{{ schema_url }}",
dom_id: '#swagger-ui',
Expand All @@ -22,7 +23,8 @@
requestInterceptor: (request) => {
request.headers['X-CSRFToken'] = "{{ csrf_token }}"
return request;
}
},
...swagger_settings
})
</script>
</body>
Expand Down
4 changes: 3 additions & 1 deletion drf_spectacular/views.py
@@ -1,3 +1,4 @@
import json
from collections import namedtuple
from typing import Any, Dict

Expand Down Expand Up @@ -83,8 +84,9 @@ def get(self, request, *args, **kwargs):
schema_url = self.url or reverse(self.url_name, request=request)
if request.GET.get('lang'):
schema_url += f'{"&" if "?" in schema_url else "?"}lang={request.GET.get("lang")}'
settings = json.dumps(spectacular_settings.SWAGGER_UI_SETTINGS)
return Response(
{'schema_url': schema_url},
{'schema_url': schema_url, 'settings': settings},
template_name=self.template_name
)

Expand Down

0 comments on commit 86dd8f7

Please sign in to comment.