diff --git a/airflow/models/dag.py b/airflow/models/dag.py index 3e6076e062455..5ed36907ad11b 100644 --- a/airflow/models/dag.py +++ b/airflow/models/dag.py @@ -718,20 +718,13 @@ def get_doc_md(self, doc_md: str | None) -> str | None: 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 0ddee31c88b94..19b618108519b 100644 --- a/tests/models/test_dag.py +++ b/tests/models/test_dag.py @@ -3120,27 +3120,25 @@ def noop_pipeline(): 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"""