Skip to content

Commit

Permalink
Add WebEncoder for trigger page rendering to avoid render failure (ap…
Browse files Browse the repository at this point in the history
  • Loading branch information
molcay authored and Artuz37 committed Aug 13, 2024
1 parent 6369fbb commit 57a25dd
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 @@ -2163,7 +2163,7 @@ def trigger(self, dag_id: str, session: Session = NEW_SESSION):
.limit(num_recent_confs)
)
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 Down Expand Up @@ -2198,6 +2198,7 @@ def trigger(self, dag_id: str, session: Session = NEW_SESSION):
},
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
from urllib.parse import quote

import pytest
Expand All @@ -28,6 +29,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 @@ -92,6 +94,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 57a25dd

Please sign in to comment.