Skip to content

Commit 8d3c8bf

Browse files
authored
Return 400 for dag_run_id with include_past/include_future in clearTaskInstances API (apache#47036)
1 parent 1370428 commit 8d3c8bf

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

airflow/api_fastapi/core_api/routes/public/task_instances.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,14 @@ def post_clear_task_instances(
630630
if dag_run is None:
631631
error_message = f"Dag Run id {dag_run_id} not found in dag {dag_id}"
632632
raise HTTPException(status.HTTP_404_NOT_FOUND, error_message)
633-
body.start_date = dag_run.logical_date
634-
body.end_date = dag_run.logical_date
633+
634+
if past or future:
635+
raise HTTPException(
636+
status.HTTP_400_BAD_REQUEST,
637+
"Cannot use include_past or include_future when dag_run_id is provided because logical_date is not applicable.",
638+
)
639+
body.start_date = dag_run.logical_date if dag_run.logical_date is not None else None
640+
body.end_date = dag_run.logical_date if dag_run.logical_date is not None else None
635641

636642
if past:
637643
body.start_date = None

tests/api_fastapi/core_api/routes/public/test_task_instances.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,6 +2062,31 @@ def test_should_respond_200(
20622062
assert response.status_code == 200
20632063
assert response.json()["total_entries"] == expected_ti
20642064

2065+
@pytest.mark.parametrize("flag", ["include_future", "include_past"])
2066+
def test_dag_run_with_future_or_past_flag_returns_400(self, test_client, session, flag):
2067+
dag_id = "example_python_operator"
2068+
payload = {
2069+
"dry_run": True,
2070+
"dag_run_id": "TEST_DAG_RUN_ID_0",
2071+
"only_failed": True,
2072+
flag: True,
2073+
}
2074+
task_instances = [{"logical_date": DEFAULT_DATETIME_1, "state": State.FAILED}]
2075+
self.create_task_instances(
2076+
session,
2077+
dag_id=dag_id,
2078+
task_instances=task_instances,
2079+
update_extras=False,
2080+
dag_run_state=State.FAILED,
2081+
)
2082+
self.dagbag.sync_to_db("dags-folder", None)
2083+
response = test_client.post(f"/public/dags/{dag_id}/clearTaskInstances", json=payload)
2084+
assert response.status_code == 400
2085+
assert (
2086+
"Cannot use include_past or include_future when dag_run_id is provided"
2087+
in response.json()["detail"]
2088+
)
2089+
20652090
@pytest.mark.parametrize(
20662091
"main_dag, task_instances, request_dag, payload, expected_ti",
20672092
[

0 commit comments

Comments
 (0)