From 916a784274989a7cfd028b721ad4fe9c12692ef3 Mon Sep 17 00:00:00 2001 From: Maksim Yermakou Date: Tue, 25 Jun 2024 14:55:51 +0000 Subject: [PATCH] Fix problem with inability to remove fields from Connection form --- airflow/www/views.py | 2 ++ tests/www/views/test_views_connection.py | 26 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/airflow/www/views.py b/airflow/www/views.py index dd8279b560071..c98c2c94a9c31 100644 --- a/airflow/www/views.py +++ b/airflow/www/views.py @@ -4180,6 +4180,8 @@ def process_form(self, form, is_created): # value isn't an empty string. if value != "": extra[field_name] = value + elif field_name in extra: + del extra[field_name] if extra.keys(): sensitive_unchanged_keys = set() for key, value in extra.items(): diff --git a/tests/www/views/test_views_connection.py b/tests/www/views/test_views_connection.py index a157e0aea0ca1..e9c60d2df60a1 100644 --- a/tests/www/views/test_views_connection.py +++ b/tests/www/views/test_views_connection.py @@ -321,6 +321,32 @@ def test_process_form_extras_updates_sensitive_placeholder_unchanged( } +@mock.patch("airflow.utils.module_loading.import_string") +@mock.patch("airflow.providers_manager.ProvidersManager.hooks", new_callable=PropertyMock) +def test_process_form_extras_remove(mock_pm_hooks, mock_import_str): + """ + Test the remove value from field. + """ + # Testing parameters set in both extra and custom fields (connection updates). + mock_form = mock.Mock() + mock_form.data = { + "conn_type": "test4", + "conn_id": "extras_test4", + "extra": '{"extra__test4__remove_field": "remove_field_val3"}', + "extra__test4__remove_field": "", + } + + cmv = ConnectionModelView() + cmv._iter_extra_field_names_and_sensitivity = mock.Mock( + return_value=[("extra__test4__remove_field", "remove_field", False)] + ) + cmv.process_form(form=mock_form, is_created=True) + + assert json.loads(mock_form.extra.data) == { + "extra__test4__remove_field": "remove_field_val3", + } + + def test_duplicate_connection(admin_client): """Test Duplicate multiple connection with suffix""" conn1 = Connection(