Skip to content

Commit

Permalink
fixup! Composer webserver patch
Browse files Browse the repository at this point in the history
Cherry-pick apache/airflow#41350

Change-Id: I7832646ecb4b95ddffc73c8b73f51fe841914015
GitOrigin-RevId: 8f83ce246ce0d8b4894961405a58dff191037fcd
  • Loading branch information
Cloud Composer Team committed Sep 16, 2024
1 parent 1dd47d2 commit e9384f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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")
Expand Down
28 changes: 28 additions & 0 deletions tests/www/views/test_views_trigger_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import datetime
import json
from decimal import Decimal

import pytest

Expand All @@ -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
Expand Down Expand Up @@ -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"

Expand Down

0 comments on commit e9384f8

Please sign in to comment.