Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WebEncoder for trigger page rendering to avoid render failure #41350

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2153,7 +2153,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 @@ -2188,6 +2188,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