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

eladkal pushed a commit to branch v3-4-test
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/v3-4-test by this push:
     new 25cc2b1f16b [v3-4-test] Fix notifier crash when callback receives a 
SerializedDAG (#74973) (#77541)
25cc2b1f16b is described below

commit 25cc2b1f16b0a0adf1348a5be1b00de846492656
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Wed Nov 25 09:05:26 2026 +0200

    [v3-4-test] Fix notifier crash when callback receives a SerializedDAG 
(#74973) (#77541)
    
    * Fix notifier crash when callback receives a dag without get_template_env
    
    When `airflow dags test` runs a failing Dag, `on_failure_callback` is
    invoked with `context["dag"]` set to a SerializedDAG, which has no
    `get_template_env` method. `Templater.get_template_env` therefore raised
    AttributeError and the notifier's `notify()` was never called, whether
    or not the notifier had template fields.
    
    Only ask the dag for its Jinja environment when it has
    `get_template_env`; otherwise fall back to the default
    SandboxedEnvironment that is already used when there is no dag. Real
    DAG objects are unaffected.
    
    Add regression tests for a templated notifier and for one without
    template fields, both given a dag object that lacks `get_template_env`.
    
    * Use a real SerializedDAG in notifier tests
    
    Build the dag passed to the notifier tests with create_scheduler_dag 
instead of a hand-made stand-in class, so the tests exercise the same 
SerializedDAG that 'airflow dags test' hands to callbacks. Both tests still 
fail with AttributeError without the hasattr guard in 
Templater.get_template_env.
    (cherry picked from commit 4dced2c8de922fdb044a1842eda5454e49ee54a9)
    
    Co-authored-by: sameer pandey 
<[email protected]>
---
 .../airflow/sdk/definitions/_internal/templater.py |  2 +-
 task-sdk/tests/task_sdk/bases/test_notifier.py     | 32 ++++++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/task-sdk/src/airflow/sdk/definitions/_internal/templater.py 
b/task-sdk/src/airflow/sdk/definitions/_internal/templater.py
index cfe4a6100e4..393f4dfb241 100644
--- a/task-sdk/src/airflow/sdk/definitions/_internal/templater.py
+++ b/task-sdk/src/airflow/sdk/definitions/_internal/templater.py
@@ -76,7 +76,7 @@ class Templater:
         # for most of the functionalities. It is imported by get_template_env()
         # though, so we don't need to put this after the 'if dag' check.
 
-        if dag:
+        if dag and hasattr(dag, "get_template_env"):
             return dag.get_template_env(force_sandboxed=False)
         return SandboxedEnvironment(cache_size=0)
 
diff --git a/task-sdk/tests/task_sdk/bases/test_notifier.py 
b/task-sdk/tests/task_sdk/bases/test_notifier.py
index b8cedaa5188..03dfe5538d5 100644
--- a/task-sdk/tests/task_sdk/bases/test_notifier.py
+++ b/task-sdk/tests/task_sdk/bases/test_notifier.py
@@ -17,6 +17,7 @@
 
 from __future__ import annotations
 
+from datetime import datetime
 from typing import TYPE_CHECKING
 from unittest.mock import MagicMock
 
@@ -27,6 +28,8 @@ from airflow.providers.standard.operators.empty import 
EmptyOperator
 from airflow.sdk.bases.notifier import BaseNotifier
 from airflow.sdk.definitions.dag import DAG
 
+from tests_common.test_utils.dag import create_scheduler_dag
+
 if TYPE_CHECKING:
     from airflow.sdk.definitions.context import Context
 
@@ -45,6 +48,13 @@ class MockNotifier(BaseNotifier):
         pass
 
 
+def _make_serialized_dag(dag_id: str):
+    """Build a real SerializedDAG, which is what `airflow dags test` passes to 
callbacks."""
+    with DAG(dag_id=dag_id, schedule=None, start_date=datetime(2026, 1, 1)) as 
dag:
+        EmptyOperator(task_id="task")
+    return create_scheduler_dag(dag)
+
+
 class TestBaseNotifier:
     def test_render_message_with_message(self):
         with DAG("test_render_message_with_message") as dag:
@@ -97,3 +107,25 @@ class TestBaseNotifier:
             }
         )
         assert notifier.message == "task: some_task"
+
+    def test_render_template_fields_with_serialized_dag(self):
+        serialized_dag = _make_serialized_dag("serialized_dag_id")
+        assert not hasattr(serialized_dag, "get_template_env")
+        notifier = MockNotifier(message="Dag {{ dag.dag_id }} failed")
+        context: Context = {"dag": serialized_dag}
+        notifier.render_template_fields(context)
+        assert notifier.message == "Dag serialized_dag_id failed"
+
+    def test_notifier_call_with_serialized_dag_and_no_template_fields(self):
+        class _BareNotifier(BaseNotifier):
+            template_fields = ()
+            template_ext = ()
+
+            def notify(self, context: Context) -> None:
+                pass
+
+        notifier = _BareNotifier()
+        notifier.notify = MagicMock()
+        context: Context = {"dag": _make_serialized_dag("bare_serialized_dag")}
+        notifier(context)
+        notifier.notify.assert_called_once()

Reply via email to