Swagger UI¶
To use Swagger UI, dist 5.22.0 has been built into the docs and a special swagger-ui.html template has been added to provide:
- Swagger UI Bundle with its
swagger-ui-bundle.jsandswagger-ui.css - Additional
swagger-ui.extra.cssto syncronize styles with Material for MkDocs - Default
deepLinkingenabled, models collapsed and singledom_idspecified
Adding a Swagger UI page¶
Required dependencies are included in the swagger-ui.html template, just
- Use this template as described below
- Initialize
SwaggerUIwith the necessary parameters
---
# title: You page title <!-- (1)! -->
template: swagger-ui.html <!-- (2)! -->
hide:
# - navigation <!-- (3)! -->
- toc <!-- (4)! -->
---
<!-- (5)! -->
<script>
SwaggerUI({
url: "openapi.json" // (6)!
});
</script>
-
Optionally title your page accodring to the
info.titlefrom yoursopenapi.jsonBy default, the page title will be the same as the name of the corresponding
navelement frommkdocs.yml -
Use this template to have a embedded
swagger-ui-bundleand custom styles applied to match Material for MkDocs theme - Consider hiding the navigation to increase the width of the Swagger UI interface
- Preferably hide the table of contents, especially if you do not have any additional content on the page
-
You can add any markdown content here if you want. Swagger UI will be displayed below this content, regardless of the script location.
Warning
The
swagger-ui.htmltemplate does not insert an implicith1header for the page, as the Swagger UI has its own header that has the same style.Therefore, if you add your own markdown content above the Swagger UI, add an explicit
# Page header -
Provide required root-related path or an external link to the
openapi.jsonand optionally configure anything you wantThe
swagger-ui.htmltemplate hasto embed single Swagger UI below page content and<div id="swagger-ui"></div>
adds this default values to theSwaggerUIconfiguration:dom_id: '#swagger-ui'for the general usecase with single Swagger UI per pagedeepLinking: true, so if for some reason you don't want to use deep links, disable them explicitlydefaultModelsExpandDepth: 0to collapse models
You can override them in this configuration object
Example
See API Reference page
Do not use SwaggerUIBundle
Always use custom SwaggerUI wrapper from the swagger-ui.html template as it is compartible with instant loading and initializes SwaggerUIBundle internally when possible
Multiple Swagger UI on a single page
By default, there is only one Swagger UI below optional markdown content per page. If you need more, add new div inside your markdown content manually, specifying its id and passing this id as dom_id to the SwaggerUI