From e9384f888f4897a2dc54354e371981771c6d1f6e Mon Sep 17 00:00:00 2001 From: Cloud Composer Team Date: Mon, 19 Aug 2024 08:10:04 +0000 Subject: [PATCH] fixup! Composer webserver patch Cherry-pick https://github.com/apache/airflow/pull/41350 Change-Id: I7832646ecb4b95ddffc73c8b73f51fe841914015 GitOrigin-RevId: 8f83ce246ce0d8b4894961405a58dff191037fcd --- airflow/www/views.py | 3 ++- tests/www/views/test_views_trigger_dag.py | 28 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/airflow/www/views.py b/airflow/www/views.py index 208cd4f6f02..fa2b5b05dd4 100644 --- a/airflow/www/views.py +++ b/airflow/www/views.py @@ -2090,7 +2090,7 @@ def trigger(self, dag_id: str, session: Session = NEW_SESSION): .limit(5) ) recent_confs = { - run_id: json.dumps(run_conf) + run_id: json.dumps(run_conf, cls=utils_json.WebEncoder) for run_id, run_conf in ((run.run_id, run.conf) for run in recent_runs) if isinstance(run_conf, dict) and any(run_conf) } @@ -2110,6 +2110,7 @@ def trigger(self, dag_id: str, session: Session = NEW_SESSION): {str(k): v.resolve(suppress_exception=True) for k, v in dag.params.items()}, indent=4, ensure_ascii=False, + cls=utils_json.WebEncoder, ) except TypeError: flash("Could not pre-populate conf field due to non-JSON-serializable data-types") diff --git a/tests/www/views/test_views_trigger_dag.py b/tests/www/views/test_views_trigger_dag.py index c48e053639f..da44dd261e0 100644 --- a/tests/www/views/test_views_trigger_dag.py +++ b/tests/www/views/test_views_trigger_dag.py @@ -19,6 +19,7 @@ import datetime import json +from decimal import Decimal import pytest @@ -27,6 +28,7 @@ from airflow.operators.empty import EmptyOperator from airflow.security import permissions from airflow.utils import timezone +from airflow.utils.json import WebEncoder from airflow.utils.session import create_session from airflow.utils.types import DagRunType from tests.test_utils.api_connexion_utils import create_test_client @@ -87,6 +89,32 @@ def test_trigger_dag_conf(admin_client): assert run.conf == conf_dict +def test_trigger_dag_conf_serializable_fields(admin_client): + test_dag_id = "example_bash_operator" + time_now = timezone.utcnow() + conf_dict = { + "string": "Hello, World!", + "date_str": "2024-08-08T09:57:35.300858", + "datetime": time_now, + "decimal": Decimal(10.465), + } + expected_conf = { + "string": "Hello, World!", + "date_str": "2024-08-08T09:57:35.300858", + "datetime": time_now.isoformat(), + "decimal": 10.465, + } + + admin_client.post(f"dags/{test_dag_id}/trigger", data={"conf": json.dumps(conf_dict, cls=WebEncoder)}) + + with create_session() as session: + run = session.query(DagRun).filter(DagRun.dag_id == test_dag_id).first() + assert run is not None + assert DagRunType.MANUAL in run.run_id + assert run.run_type == DagRunType.MANUAL + assert run.conf == expected_conf + + def test_trigger_dag_conf_malformed(admin_client): test_dag_id = "example_bash_operator"