Skip to content

Commit d84a3c3

Browse files
committed
Added support for Swagger UI initOAuth method (#4)
1 parent fff7967 commit d84a3c3

File tree

5 files changed

+71
-13
lines changed

5 files changed

+71
-13
lines changed

CHANGELOG

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
mkdocs-swagger-ui-tag 0.4.0 (2022-07-31)
2+
3+
* Added support for Swagger UI initOAuth method (#4)
4+
* Updated swagger-ui-dist to 4.13.0
5+
16
mkdocs-swagger-ui-tag 0.3.2 (2022-07-16)
27

38
* Fixed #2: Incompatible with mkdocs-material Instant loading feature

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ A MkDocs plugin supports for add [Swagger UI](https://github.com/swagger-api/swa
1414
3. Synchronized dark mode with [mkdocs-material](https://squidfunk.github.io/mkdocs-material/)
1515
4. Configure [Swagger UI configuration](https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/) through plugin options and tag attributes
1616
5. Support multiple OAS in single Swagger UI with top bar selector
17+
6. Support Swagger UI [initOAuth](https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/) method
1718

1819
## Dependency
1920

2021
1. Python Package
21-
1. beautifulsoup4==4.11.1
22+
1. beautifulsoup4>=4.11.1
2223
2. [Swagger UI dist](https://www.npmjs.com/package/swagger-ui-dist) javascript file and css file
23-
1. swagger-ui-dist==4.12.0
24+
1. swagger-ui-dist>=4.13.0
2425

2526
## Usage
2627

mkdocs_swagger_ui_tag/plugin.py

+56-10
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def on_post_page(self, output, page, config, **kwargs):
110110
iframe_filename = f"swagger-{cur_id}.html"
111111
iframe_id_list.append(cur_id)
112112
cur_options = self.process_options(swagger_ui_ele)
113+
cur_oath2_prop = self.process_oath2_prop(swagger_ui_ele)
113114

114115
openapi_spec_url = self.path_to_url(page.file,
115116
swagger_ui_ele.get('src', ""))
@@ -119,7 +120,8 @@ def on_post_page(self, output, page, config, **kwargs):
119120
background=self.config['background'],
120121
id=cur_id,
121122
openapi_spec_url=openapi_spec_url,
122-
options=json.dumps(cur_options, indent=4)[1:-1])
123+
options=json.dumps(cur_options, indent=4)[1:-1],
124+
oath2_prop=json.dumps(cur_oath2_prop))
123125
with open(os.path.join(page_dir, iframe_filename), 'w') as f:
124126
f.write(output_from_parsed_template)
125127
self.replace_with_iframe(soup, swagger_ui_ele, cur_id,
@@ -139,13 +141,15 @@ def on_post_page(self, output, page, config, **kwargs):
139141

140142
# only use options from first grouped swagger ui tag
141143
cur_options = self.process_options(grouped_list[0])
144+
cur_oath2_prop = self.process_oath2_prop(grouped_list[0])
142145
output_from_parsed_template = template.render(
143146
css_dir=css_dir,
144147
js_dir=js_dir,
145148
background=self.config['background'],
146149
id=cur_id,
147150
openapi_spec_url=openapi_spec_url,
148-
options=json.dumps(cur_options, indent=4)[1:-1])
151+
options=json.dumps(cur_options, indent=4)[1:-1],
152+
oath2_prop=json.dumps(cur_oath2_prop))
149153
with open(os.path.join(page_dir, iframe_filename), 'w') as f:
150154
f.write(output_from_parsed_template)
151155
self.replace_with_iframe(soup, grouped_list[0], cur_id,
@@ -227,18 +231,22 @@ def process_options(self, swagger_ui_ele):
227231
options_keys = global_options.keys()
228232
cur_options = {}
229233
for k in options_keys:
230-
cur_val = swagger_ui_ele.get(k.lower(), None)
231-
if cur_val is not None:
234+
val = swagger_ui_ele.get(k.lower(), None)
235+
if val is not None:
232236
if k == "supportedSubmitMethods":
233237
try:
234-
cur_val = json.loads(cur_val.replace("'", '"'))
235-
if not isinstance(cur_val, list):
238+
val = json.loads(val.replace("'", '"'))
239+
if not isinstance(val, list):
236240
raise ValueError(
237-
f"attribute supportedSubmitMethods: {cur_val} is not a list."
241+
f"Attribute supportedSubmitMethods: {val} is not a list."
238242
)
239-
except:
240-
cur_val = global_options['supportedSubmitMethods']
241-
cur_options[k] = cur_val
243+
cur_options[k] = val
244+
except Exception as e:
245+
logging.error(e)
246+
logging.error(
247+
"Ignore supportedSubmitMethods attribute setting.")
248+
else:
249+
cur_options[k] = val
242250
else:
243251
cur_options[k] = global_options[k]
244252
if cur_options[k] is None:
@@ -248,6 +256,44 @@ def process_options(self, swagger_ui_ele):
248256
"syntaxHighlightTheme")
249257
return cur_options
250258

259+
def process_oath2_prop(self, swagger_ui_ele):
260+
""" Retrieve Swagger UI OAuth 2.0 configuration from tag attributes """
261+
oath_prop_keys = [
262+
"clientId",
263+
"clientSecret",
264+
"realm",
265+
"appName",
266+
"scopes",
267+
"additionalQueryStringParams",
268+
"useBasicAuthenticationWithAccessCodeGrant",
269+
"usePkceWithAuthorizationCodeGrant",
270+
]
271+
cur_prop = {}
272+
for k in oath_prop_keys:
273+
val = swagger_ui_ele.get(k.lower(), None)
274+
if val is not None:
275+
if k == "additionalQueryStringParams":
276+
try:
277+
val = json.loads(val.replace("'", '"'))
278+
if not isinstance(val, dict):
279+
raise ValueError(
280+
f"attribute additionalQueryStringParams: {val} is not a dict."
281+
)
282+
cur_prop[k] = val
283+
except Exception as e:
284+
logging.error(e)
285+
logging.error(
286+
"Ignore additionalQueryStringParams attribute setting."
287+
)
288+
elif k in [
289+
"useBasicAuthenticationWithAccessCodeGrant",
290+
"usePkceWithAuthorizationCodeGrant"
291+
]:
292+
cur_prop[k] = val.lower().strip() == "true"
293+
else:
294+
cur_prop[k] = val
295+
return cur_prop
296+
251297
def on_post_build(self, config, **kwargs):
252298
""" Copy Swagger UI css and js files to assets directory """
253299

mkdocs_swagger_ui_tag/swagger-ui/swagger.html

+6
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,17 @@
3232
{% endif %}
3333
{{options}}
3434
})
35+
36+
{% if oath2_prop != "{}" %}
37+
window.ui.initOAuth({{oath2_prop}})
38+
{% endif %}
39+
3540
const scheme = parent.scheme
3641
if (scheme === "slate") {
3742
enable_dark_mode();
3843
}
3944
}
45+
4046
const resize_ob = new ResizeObserver(function(entries) {
4147
parent.update_swagger_ui_iframe_height("{{id}}");
4248
});

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
long_description = f.read()
55

66
setup(name="mkdocs-swagger-ui-tag",
7-
version="0.3.2",
7+
version="0.4.0",
88
author="Blueswen",
99
author_email="[email protected]",
1010
url="https://blueswen.github.io/mkdocs-swagger-ui-tag",

0 commit comments

Comments
 (0)