From 0de169f60129be6e2ca74ffd856f2f52c39bd309 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Tue, 20 Feb 2024 11:57:50 +0300 Subject: [PATCH 1/6] fix: changed absolute static prefix to relative --- swagger_ui/core.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/swagger_ui/core.py b/swagger_ui/core.py index 8279371..c7df19b 100644 --- a/swagger_ui/core.py +++ b/swagger_ui/core.py @@ -79,8 +79,10 @@ def static_dir(self): @property def doc_html(self): + prefix_split = self.url_prefix.split("/") + static_location = prefix_split.pop() return self.env.get_template('doc.html').render( - url_prefix=self.url_prefix, + url_prefix=static_location, title=self.title, config_url=self.swagger_json_uri_absolute, parameters=self.parameters, From 0c0cec7cf1faa677fc0a355890553097e19c4ea3 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Tue, 20 Feb 2024 12:43:54 +0300 Subject: [PATCH 2/6] fix: set relative swagger url --- swagger_ui/core.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/swagger_ui/core.py b/swagger_ui/core.py index c7df19b..f3fe2c3 100644 --- a/swagger_ui/core.py +++ b/swagger_ui/core.py @@ -3,13 +3,10 @@ import urllib.request from pathlib import Path -from jinja2 import Environment -from jinja2 import FileSystemLoader -from jinja2 import select_autoescape +from jinja2 import Environment, FileSystemLoader, select_autoescape from swagger_ui.handlers import supported_list -from swagger_ui.utils import SWAGGER_UI_PY_ROOT -from swagger_ui.utils import _load_config +from swagger_ui.utils import SWAGGER_UI_PY_ROOT, _load_config _DefaultSwaggerUIBundleParameters = { "dom_id": "\"#swagger-ui\"", @@ -49,8 +46,10 @@ def __init__(self, self.config_path = config_path self.config_spec = config_spec self.config_rel_url = config_rel_url - assert (self.config or self.config_url or self.config_path or self.config_spec or - self.config_rel_url), \ + assert ( + self.config or self.config_url or self.config_path or + self.config_spec or + self.config_rel_url), \ 'One of arguments "config", "config_path", "config_url", "config_spec"' \ ' or "config_rel_url" is required!' @@ -58,7 +57,7 @@ def __init__(self, self.parameters = copy.deepcopy(_DefaultSwaggerUIBundleParameters) if parameters: self.parameters.update(parameters) - self.parameters["url"] = "\"{}\"".format(self.swagger_json_uri_absolute) + self.set_swagger_url() # oauth2_config self.oauth2_config = oauth2_config @@ -164,3 +163,12 @@ def match(name): if handler: return handler return None + + def set_swagger_url(self): + """Set relative swagger url.""" + swagger_url_absolute = self.swagger_json_uri_absolute + split_path = swagger_url_absolute.split("/") + file_name = split_path.pop() + relative_prefix = split_path.pop() + swagger_url_relative = "/".join([relative_prefix, file_name]) + self.parameters["url"] = "\"{}\"".format(swagger_url_relative) From 5babb4f089182d24dc55cfe2b2930b1658b52409 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Tue, 20 Feb 2024 12:45:42 +0300 Subject: [PATCH 3/6] chore: revert formatting --- swagger_ui/core.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/swagger_ui/core.py b/swagger_ui/core.py index f3fe2c3..7af8825 100644 --- a/swagger_ui/core.py +++ b/swagger_ui/core.py @@ -3,10 +3,13 @@ import urllib.request from pathlib import Path -from jinja2 import Environment, FileSystemLoader, select_autoescape +from jinja2 import Environment +from jinja2 import FileSystemLoader +from jinja2 import select_autoescape from swagger_ui.handlers import supported_list -from swagger_ui.utils import SWAGGER_UI_PY_ROOT, _load_config +from swagger_ui.utils import SWAGGER_UI_PY_ROOT +from swagger_ui.utils import _load_config _DefaultSwaggerUIBundleParameters = { "dom_id": "\"#swagger-ui\"", @@ -46,10 +49,8 @@ def __init__(self, self.config_path = config_path self.config_spec = config_spec self.config_rel_url = config_rel_url - assert ( - self.config or self.config_url or self.config_path or - self.config_spec or - self.config_rel_url), \ + assert (self.config or self.config_url or self.config_path or self.config_spec or + self.config_rel_url), \ 'One of arguments "config", "config_path", "config_url", "config_spec"' \ ' or "config_rel_url" is required!' From de8a4f4715ecdb2fe1826cfa927c0be1d40bfaee Mon Sep 17 00:00:00 2001 From: Oleg A Date: Tue, 20 Feb 2024 12:47:02 +0300 Subject: [PATCH 4/6] chore: use f-string --- swagger_ui/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swagger_ui/core.py b/swagger_ui/core.py index 7af8825..b5f404c 100644 --- a/swagger_ui/core.py +++ b/swagger_ui/core.py @@ -172,4 +172,4 @@ def set_swagger_url(self): file_name = split_path.pop() relative_prefix = split_path.pop() swagger_url_relative = "/".join([relative_prefix, file_name]) - self.parameters["url"] = "\"{}\"".format(swagger_url_relative) + self.parameters["url"] = f'"{swagger_url_relative}"' From f7583173678666c1a856c4585d0c17d927780e2a Mon Sep 17 00:00:00 2001 From: Oleg A Date: Tue, 20 Feb 2024 12:54:51 +0300 Subject: [PATCH 5/6] chore: rm redundant var --- swagger_ui/core.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/swagger_ui/core.py b/swagger_ui/core.py index b5f404c..ea43c08 100644 --- a/swagger_ui/core.py +++ b/swagger_ui/core.py @@ -167,8 +167,7 @@ def match(name): def set_swagger_url(self): """Set relative swagger url.""" - swagger_url_absolute = self.swagger_json_uri_absolute - split_path = swagger_url_absolute.split("/") + split_path = self.swagger_json_uri_absolute.split("/") file_name = split_path.pop() relative_prefix = split_path.pop() swagger_url_relative = "/".join([relative_prefix, file_name]) From 68a438d70a1eb7ff6fb011c01d911e7cbf1eea0c Mon Sep 17 00:00:00 2001 From: Oleg A Date: Tue, 20 Feb 2024 12:57:10 +0300 Subject: [PATCH 6/6] chore: refactor split constructor --- swagger_ui/core.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/swagger_ui/core.py b/swagger_ui/core.py index ea43c08..d2fa13a 100644 --- a/swagger_ui/core.py +++ b/swagger_ui/core.py @@ -168,7 +168,5 @@ def match(name): def set_swagger_url(self): """Set relative swagger url.""" split_path = self.swagger_json_uri_absolute.split("/") - file_name = split_path.pop() - relative_prefix = split_path.pop() - swagger_url_relative = "/".join([relative_prefix, file_name]) + swagger_url_relative = "/".join(split_path[-2:]) self.parameters["url"] = f'"{swagger_url_relative}"'