This is an automated email from the ASF dual-hosted git repository. utkarsharma pushed a commit to branch v2-9-test in repository https://gitbox.apache.org/repos/asf/airflow.git
commit e9ddb067163f399c127d652b1bdf633bf442f04f Author: Wei Lee <[email protected]> AuthorDate: Tue Jul 2 17:04:40 2024 +0800 fix: disable jinja2 rednering for doc_md (#40522) (cherry picked from commit 8159f6e24704f5e0e3b3217cf79ecf5083dce531) --- airflow/models/dag.py | 19 ++++++------------- tests/models/test_dag.py | 16 +++++++--------- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/airflow/models/dag.py b/airflow/models/dag.py index 3e6076e062..5ed36907ad 100644 --- a/airflow/models/dag.py +++ b/airflow/models/dag.py @@ -718,20 +718,13 @@ class DAG(LoggingMixin): if doc_md is None: return doc_md - env = self.get_template_env(force_sandboxed=True) - - if not doc_md.endswith(".md"): - template = jinja2.Template(doc_md) - else: + if doc_md.endswith(".md"): try: - template = env.get_template(doc_md) - except jinja2.exceptions.TemplateNotFound: - return f""" - # Templating Error! - Not able to find the template file: `{doc_md}`. - """ - - return template.render() + return open(doc_md).read() + except FileNotFoundError: + return doc_md + + return doc_md def _check_schedule_interval_matches_timetable(self) -> bool: """Check ``schedule_interval`` and ``timetable`` match. diff --git a/tests/models/test_dag.py b/tests/models/test_dag.py index 0ddee31c88..19b6181085 100644 --- a/tests/models/test_dag.py +++ b/tests/models/test_dag.py @@ -3120,27 +3120,25 @@ class TestDagDecorator: assert dag.dag_id == "noop_pipeline" assert "Regular DAG documentation" in dag.doc_md - def test_resolve_documentation_template_file_rendered(self, tmp_path): + def test_resolve_documentation_template_file_not_rendered(self, tmp_path): """Test that @dag uses function docs as doc_md for DAG object""" - path = tmp_path / "testfile.md" - path.write_text( - """ + raw_content = """ {% if True %} External Markdown DAG documentation {% endif %} """ - ) - @dag_decorator( - "test-dag", start_date=DEFAULT_DATE, template_searchpath=os.fspath(path.parent), doc_md=path.name - ) + path = tmp_path / "testfile.md" + path.write_text(raw_content) + + @dag_decorator("test-dag", start_date=DEFAULT_DATE, doc_md=str(path)) def markdown_docs(): ... dag = markdown_docs() assert isinstance(dag, DAG) assert dag.dag_id == "test-dag" - assert dag.doc_md.strip() == "External Markdown DAG documentation" + assert dag.doc_md == raw_content def test_fails_if_arg_not_set(self): """Test that @dag decorated function fails if positional argument is not set"""
