Skip to content

Commit 5395c9d

Browse files
github-actions[bot]tirkarthi
authored andcommitted
[v3-1-test] Handle non-dictionary json payload during logging to avoid internal server error. (#62355) (#62367)
(cherry picked from commit 46a433d) Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
1 parent 6c4cfa5 commit 5395c9d

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

airflow-core/src/airflow/api_fastapi/logging/decorators.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,16 @@ async def log_action(
9191
user_display = user.get_name()
9292

9393
has_json_body = "application/json" in request.headers.get("content-type", "") and await request.body()
94+
request_body = {}
95+
masked_body_json = {}
9496

9597
if has_json_body:
9698
request_body = await request.json()
97-
masked_body_json = {k: secrets_masker.redact(v, k) for k, v in request_body.items()}
98-
else:
99-
request_body = {}
100-
masked_body_json = {}
99+
if isinstance(request_body, dict):
100+
masked_body_json = {k: secrets_masker.redact(v, k) for k, v in request_body.items()}
101101

102-
if event_name in skip_dry_run_events and request_body.get("dry_run", True):
103-
return
102+
if event_name in skip_dry_run_events and request_body.get("dry_run", True):
103+
return
104104

105105
fields_skip_logging = {
106106
"csrf_token",

airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_dag_run.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,11 +1656,25 @@ def test_should_respond_403(self, unauthorized_test_client):
16561656
]
16571657
},
16581658
),
1659+
(
1660+
[],
1661+
{
1662+
"detail": [
1663+
{
1664+
"type": "model_attributes_type",
1665+
"loc": ["body"],
1666+
"msg": "Input should be a valid dictionary or object to extract fields from",
1667+
"input": [],
1668+
}
1669+
]
1670+
},
1671+
),
16591672
],
16601673
)
16611674
def test_invalid_data(self, test_client, post_body, expected_detail):
1662-
now = timezone.utcnow().isoformat()
1663-
post_body["logical_date"] = now
1675+
if isinstance(post_body, dict):
1676+
now = timezone.utcnow().isoformat()
1677+
post_body["logical_date"] = now
16641678
response = test_client.post(f"/dags/{DAG1_ID}/dagRuns", json=post_body)
16651679
assert response.status_code == 422
16661680
assert response.json() == expected_detail

0 commit comments

Comments
 (0)