From 7c81cb561746bd7f37c902541bdc4c7a3534f9bd Mon Sep 17 00:00:00 2001 From: pierrejeambrun Date: Thu, 7 Aug 2025 19:21:06 +0200 Subject: [PATCH] Connection Extra additional test case for validation --- .../routes/public/test_connections.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_connections.py b/airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_connections.py index 967f8abc08c03..d87459e8df346 100644 --- a/airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_connections.py +++ b/airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_connections.py @@ -1258,3 +1258,36 @@ def test_post_should_accept_empty_string_as_extra(self, test_client, session): connection = session.query(Connection).filter_by(conn_id=TEST_CONN_ID).first() assert connection is not None assert connection.extra == "{}" # Backward compatibility: treat "" as empty JSON object + + @pytest.mark.parametrize( + "extra, expected_error_message", + [ + ("[1,2,3]", "Expected JSON object in `extra` field, got non-dict JSON"), + ("some_string", "Encountered non-JSON in `extra` field"), + ], + ) + def test_post_should_fail_with_non_json_object_as_extra( + self, test_client, extra, expected_error_message, session + ): + """JSON primitives are a valid JSON and should raise 422 validation error.""" + body = {"connection_id": TEST_CONN_ID, "conn_type": TEST_CONN_TYPE, "extra": extra} + + response = test_client.post("/connections", json=body) + assert response.status_code == 422 + assert ( + "Value error, The `extra` field must be a valid JSON object (e.g., {'key': 'value'})" + in response.json()["detail"][0]["msg"] + ) + + _check_last_log( + session, + dag_id=None, + event="post_connection", + logical_date=None, + expected_extra={ + "connection_id": "test_connection_id", + "conn_type": "test_type", + "extra": expected_error_message, + "method": "POST", + }, + )