Skip to content

Commit

Permalink
Fix passing redoc UI parameters
Browse files Browse the repository at this point in the history
Currently passing redoc UI parameters has the following issues:
* It puts json-ed values of parameters in single quotes, which will
  break if the value itself contains single quotes.
* If the value is falsy, only the name of the parameter is used. This
  syntax is *not* treated as falsy by redoc.

The latter cases the download button to be always hidden, because the
default parameters have "hide-download-button": False. There is no way
to un-hide it with the current code.

Change this to use the javascript API to pass parameters[1], which is
much simpler and more robust.

Also, stop using the "next" version of redoc, as suggested here[2]. This
was an old release candidate for version 2. Use the officially released
version 2.

[1] https://redocly.com/docs/redoc/deployment/html/#the-redoc-object
[2] fastapi/fastapi#9700
  • Loading branch information
eltoder committed Jun 13, 2024
1 parent 0bd7fa3 commit d23df94
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 @@ -10,7 +10,8 @@ def test_redoc_plain() -> None:
response = 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


def test_redoc_custom() -> None:
Expand Down

0 comments on commit d23df94

Please sign in to comment.