Skip to content

Commit

Permalink
Pass ReDoc UI Parameters using the JavaScript API instead of HTML Tags (
Browse files Browse the repository at this point in the history
#25)

Pass ReDoc UI Parameters using the JavaScript API instead of HTML Tags
  • Loading branch information
eltoder authored Jul 22, 2024
1 parent d973c13 commit 45257de
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
24 changes: 11 additions & 13 deletions src/openapipages/redoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"theme": {
"typography": {"code": {"wrap": True}},
},
"hide-download-button": False,
"hideDownloadButton": False,
}


Expand All @@ -35,7 +35,7 @@ class ReDoc(Base):
It is normally set to a CDN URL.
"""
),
] = "https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"
] = "https://cdn.jsdelivr.net/npm/redoc@2/bundles/redoc.standalone.js"
with_google_fonts: Annotated[
bool,
Doc(
Expand Down Expand Up @@ -64,15 +64,6 @@ def render(self) -> str:
current_redoc_ui_parameters = default_parameters.copy()
current_redoc_ui_parameters.update(self.ui_parameters or {})

def add_redoc_ui_parameters() -> str:
_props = ""
for key, value in current_redoc_ui_parameters.items():
if value:
_props += f"{key}='{json.dumps(value)}' "
else:
_props += f"{key} "
return _props

html_template = self.get_html_template()
return html_template.format(
title=self.title,
Expand All @@ -82,7 +73,7 @@ def add_redoc_ui_parameters() -> str:
head_js_str=self.get_head_js_str(),
tail_js_str=self.get_tail_js_str(),
google_fonts_str=google_fonts_str,
redoc_ui_parameters=add_redoc_ui_parameters(),
redoc_ui_parameters=json.dumps(current_redoc_ui_parameters, indent=2),
)

def get_html_template(self) -> str:
Expand All @@ -109,8 +100,15 @@ def get_html_template(self) -> str:
<noscript>
ReDoc requires Javascript to function. Please enable it to browse the documentation.
</noscript>
<redoc spec-url="{openapi_url}" {redoc_ui_parameters}></redoc>
<div id="redoc-container"></div>
{tail_js_str}
<script>
Redoc.init(
"{openapi_url}",
{redoc_ui_parameters},
document.getElementById("redoc-container")
)
</script>
</body>
</html>
"""
Expand Down
3 changes: 2 additions & 1 deletion tests/test_redoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ async def test_redoc_plain() -> None:
response = await client.get("/redoc-plain")
assert response.status_code == 200, response.text
assert response.headers["content-type"] == "text/html; charset=utf-8"
assert "redoc@next" in response.text
assert "redoc@2" in response.text
assert '"hideDownloadButton": false' in response.text


@pytest.mark.asyncio()
Expand Down

0 comments on commit 45257de

Please sign in to comment.