Skip to content

Commit 1b4274d

Browse files
authored
Merge pull request #171 from joshua-harrison-2011/swagger_ui_config
Adding support for OPENAPI_SWAGGER_UI_CONFIG
2 parents 2a793ad + 7b4c1d6 commit 1b4274d

4 files changed

Lines changed: 19 additions & 24 deletions

File tree

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ Contributors (chronological)
1616
- Steven Loria `@sloria <https://github.com/sloria>`_
1717
- Jón Bjarnason `@nonnib <https://github.com/nonnib>`_
1818
- Igor Davydenko `@playpauseandstop <https://github.com/playpauseandstop>`_
19+
- Joshua Harrison `@joshua-harrison-2011 <https://github.com/joshua-harrison-2011>`_

docs/openapi.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,15 @@ page using the JS script from the script URL.
303303

304304
Default: ``None``
305305

306-
.. describe:: OPENAPI_SWAGGER_UI_SUPPORTED_SUBMIT_METHODS
306+
.. describe:: OPENAPI_SWAGGER_UI_CONFIG
307307

308-
List of methods for which the '*Try it out!*' feature is enabled. Should be a
309-
list of lowercase HTTP methods.
308+
Dictionary representing Swagger UI configuration options. See `Swagger UI Configuration`_ for available options.
309+
All JSON serializable options are supported.
310310

311-
Passing an empty list disables the feature globally.
311+
Examples:
312+
* ``{'deepLinking': True, 'supportedSubmitMethods': ['get', 'post']}``
312313

313-
Default: ``['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace']``
314+
Default: ``{}``
314315

315316
Here's an example application configuration using both ReDoc and Swagger UI:
316317

@@ -327,6 +328,7 @@ Here's an example application configuration using both ReDoc and Swagger UI:
327328
328329
.. _ReDoc: https://github.com/Rebilly/ReDoc
329330
.. _Swagger UI: https://swagger.io/tools/swagger-ui/
331+
.. _Swagger UI Configuration: https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/
330332

331333
Write OpenAPI Documentation File
332334
--------------------------------

flask_smorest/spec/__init__.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,12 @@ def _register_swagger_ui_rule(self, blueprint):
7272
7373
The Swagger UI scripts base URL should be specified as
7474
OPENAPI_SWAGGER_UI_URL.
75-
76-
OPENAPI_SWAGGER_UI_SUPPORTED_SUBMIT_METHODS specifes the methods for
77-
which the 'Try it out!' feature is enabled.
7875
"""
7976
swagger_ui_path = self._app.config.get('OPENAPI_SWAGGER_UI_PATH')
8077
if swagger_ui_path is not None:
8178
swagger_ui_url = self._app.config.get('OPENAPI_SWAGGER_UI_URL')
8279
if swagger_ui_url is not None:
8380
self._swagger_ui_url = swagger_ui_url
84-
self._swagger_ui_supported_submit_methods = (
85-
self._app.config.get(
86-
'OPENAPI_SWAGGER_UI_SUPPORTED_SUBMIT_METHODS',
87-
['get', 'put', 'post', 'delete', 'options',
88-
'head', 'patch', 'trace'])
89-
)
9081
blueprint.add_url_rule(
9182
_add_leading_slash(swagger_ui_path),
9283
endpoint='openapi_swagger_ui',
@@ -110,8 +101,7 @@ def _openapi_swagger_ui(self):
110101
return flask.render_template(
111102
'swagger_ui.html', title=self.spec.title,
112103
swagger_ui_url=self._swagger_ui_url,
113-
swagger_ui_supported_submit_methods=(
114-
self._swagger_ui_supported_submit_methods)
104+
swagger_ui_config=self._app.config.get('OPENAPI_SWAGGER_UI_CONFIG', {})
115105
)
116106

117107

flask_smorest/spec/templates/swagger_ui.html

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313
<script src="{{swagger_ui_url}}swagger-ui-standalone-preset.js"></script>
1414
<script src="{{swagger_ui_url}}swagger-ui-bundle.js"></script>
1515
<script>
16+
17+
config = {
18+
url: "{{ url_for('api-docs.openapi_json') }}",
19+
dom_id: '#swagger-ui-container'
20+
}
21+
22+
var override_config = {{ swagger_ui_config | tojson }};
23+
for (var attrname in override_config) { config[attrname] = override_config[attrname]; }
24+
1625
window.onload = function() {
17-
const ui = SwaggerUIBundle({
18-
url: "{{ url_for('api-docs.openapi_json') }}",
19-
dom_id: '#swagger-ui-container',
20-
deepLinking: true,
21-
layout: "BaseLayout",
22-
supportedSubmitMethods: [{% for meth in swagger_ui_supported_submit_methods %}"{{meth.lower()}}",{% endfor %}],
23-
})
24-
window.ui = ui
26+
window.ui = SwaggerUIBundle(config)
2527
}
2628
</script>
2729

0 commit comments

Comments
 (0)