Skip to content

Commit fbea13e

Browse files
HsiuChuanHsuTyrellHaywood
authored andcommitted
Fix: Correctly parse JSON for --dag_run_conf in airflow dags backfill CLI (apache#56380)
* feat: Parse and validate JSON for --dag-run-conf in backfill command Add import json and update the argument parsing in create_backfill. * unit tests covering valid JSON, invalid JSON, and empty JSON cases
1 parent 93cab99 commit fbea13e

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

airflow-core/src/airflow/cli/commands/backfill_command.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from __future__ import annotations
1919

20+
import json
2021
import logging
2122
import signal
2223

@@ -80,13 +81,22 @@ def create_backfill(args) -> None:
8081
except AirflowConfigException as e:
8182
log.warning("Failed to get user name from os: %s, not setting the triggering user", e)
8283
user = None
84+
85+
# Parse dag_run_conf if provided
86+
dag_run_conf = None
87+
if args.dag_run_conf:
88+
try:
89+
dag_run_conf = json.loads(args.dag_run_conf)
90+
except json.JSONDecodeError as e:
91+
raise ValueError(f"Invalid JSON in --dag-run-conf: {e}")
92+
8393
_create_backfill(
8494
dag_id=args.dag_id,
8595
from_date=args.from_date,
8696
to_date=args.to_date,
8797
max_active_runs=args.max_active_runs,
8898
reverse=args.run_backwards,
89-
dag_run_conf=args.dag_run_conf,
99+
dag_run_conf=dag_run_conf,
90100
triggering_user_name=user,
91101
reprocess_behavior=reprocess_behavior,
92102
run_on_latest_version=args.run_on_latest_version,

airflow-core/tests/unit/cli/commands/test_backfill_command.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,78 @@ def test_backfill_dry_run(self, mock_dry_run, reverse):
162162
reprocess_behavior="none",
163163
session=mock.ANY,
164164
)
165+
166+
@mock.patch("airflow.cli.commands.backfill_command._create_backfill")
167+
def test_backfill_with_dag_run_conf(self, mock_create):
168+
"""Test that dag_run_conf is properly parsed from JSON string."""
169+
args = [
170+
"backfill",
171+
"create",
172+
"--dag-id",
173+
"example_bash_operator",
174+
"--from-date",
175+
DEFAULT_DATE.isoformat(),
176+
"--to-date",
177+
DEFAULT_DATE.isoformat(),
178+
"--dag-run-conf",
179+
'{"example_key": "example_value"}',
180+
]
181+
airflow.cli.commands.backfill_command.create_backfill(self.parser.parse_args(args))
182+
183+
mock_create.assert_called_once_with(
184+
dag_id="example_bash_operator",
185+
from_date=DEFAULT_DATE,
186+
to_date=DEFAULT_DATE,
187+
max_active_runs=None,
188+
reverse=False,
189+
dag_run_conf={"example_key": "example_value"},
190+
reprocess_behavior=None,
191+
triggering_user_name="root",
192+
run_on_latest_version=False,
193+
)
194+
195+
def test_backfill_with_invalid_dag_run_conf(self):
196+
"""Test that invalid JSON in dag_run_conf raises ValueError."""
197+
args = [
198+
"backfill",
199+
"create",
200+
"--dag-id",
201+
"example_bash_operator",
202+
"--from-date",
203+
DEFAULT_DATE.isoformat(),
204+
"--to-date",
205+
DEFAULT_DATE.isoformat(),
206+
"--dag-run-conf",
207+
'{"invalid": json}', # Invalid JSON
208+
]
209+
with pytest.raises(ValueError, match="Invalid JSON in --dag-run-conf"):
210+
airflow.cli.commands.backfill_command.create_backfill(self.parser.parse_args(args))
211+
212+
@mock.patch("airflow.cli.commands.backfill_command._create_backfill")
213+
def test_backfill_with_empty_dag_run_conf(self, mock_create):
214+
"""Test that empty dag_run_conf is properly parsed."""
215+
args = [
216+
"backfill",
217+
"create",
218+
"--dag-id",
219+
"example_bash_operator",
220+
"--from-date",
221+
DEFAULT_DATE.isoformat(),
222+
"--to-date",
223+
DEFAULT_DATE.isoformat(),
224+
"--dag-run-conf",
225+
"{}",
226+
]
227+
airflow.cli.commands.backfill_command.create_backfill(self.parser.parse_args(args))
228+
229+
mock_create.assert_called_once_with(
230+
dag_id="example_bash_operator",
231+
from_date=DEFAULT_DATE,
232+
to_date=DEFAULT_DATE,
233+
max_active_runs=None,
234+
reverse=False,
235+
dag_run_conf={},
236+
reprocess_behavior=None,
237+
triggering_user_name="root",
238+
run_on_latest_version=False,
239+
)

0 commit comments

Comments
 (0)