From 2229831e80f55e1f8bfb536f73bc77013566df21 Mon Sep 17 00:00:00 2001 From: Robbe Sneyders Date: Sat, 24 Jul 2021 21:34:45 +0200 Subject: [PATCH] Render correct template in API (#1404) * Explicitly render templates of correct blueprint * Remove unused constant --- connexion/apis/flask_api.py | 9 ++++++++- connexion/options.py | 2 -- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/connexion/apis/flask_api.py b/connexion/apis/flask_api.py index 93bea8caf..b90c3da6e 100644 --- a/connexion/apis/flask_api.py +++ b/connexion/apis/flask_api.py @@ -4,6 +4,7 @@ """ import logging +import pathlib import warnings from typing import Any @@ -296,7 +297,13 @@ def console_ui_home(self): } if self.options.openapi_console_ui_config is not None: template_variables['configUrl'] = 'swagger-ui-config.json' - return flask.render_template('index.j2', **template_variables) + + # Use `render_template_string` instead of `render_template` to circumvent the flask + # template lookup mechanism and explicitly render the template of the current blueprint. + # https://github.com/zalando/connexion/issues/1289#issuecomment-884105076 + template_dir = pathlib.Path(self.options.openapi_console_ui_from_dir) + index_path = template_dir / 'index.j2' + return flask.render_template_string(index_path.read_text(), **template_variables) def console_ui_static_files(self, filename): """ diff --git a/connexion/options.py b/connexion/options.py index ff7112c12..c2a2682e2 100644 --- a/connexion/options.py +++ b/connexion/options.py @@ -3,7 +3,6 @@ """ import logging -import pathlib from typing import Optional # NOQA try: @@ -13,7 +12,6 @@ from connexion.decorators.uri_parsing import AbstractURIParser -MODULE_PATH = pathlib.Path(__file__).absolute().parent NO_UI_MSG = """The swagger_ui directory could not be found. Please install connexion with extra install: pip install connexion[swagger-ui] or provide the path to your local installation by passing swagger_path=