This is an automated email from the ASF dual-hosted git repository.

ephraimanierobi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 8159f6e247 fix: disable jinja2 rednering for doc_md (#40522)
8159f6e247 is described below

commit 8159f6e24704f5e0e3b3217cf79ecf5083dce531
Author: Wei Lee <[email protected]>
AuthorDate: Tue Jul 2 17:04:40 2024 +0800

    fix: disable jinja2 rednering for doc_md (#40522)
---
 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 560d05b548..9cdcfaef6d 100644
--- a/airflow/models/dag.py
+++ b/airflow/models/dag.py
@@ -772,20 +772,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:
         """
diff --git a/tests/models/test_dag.py b/tests/models/test_dag.py
index 52001bef91..bae89a645a 100644
--- a/tests/models/test_dag.py
+++ b/tests/models/test_dag.py
@@ -3274,27 +3274,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"""

Reply via email to