Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update custom connection field processing #20883

Merged
merged 2 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 17 additions & 11 deletions airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3594,19 +3594,17 @@ def process_form(self, form, is_created):
"""Process form data."""
conn_type = form.data['conn_type']
conn_id = form.data["conn_id"]
extra = {
key: form.data[key]
for key in self.extra_fields
if key in form.data and key.startswith(f"extra__{conn_type}__")
}

# If parameters are added to the classic `Extra` field, include these values along with
# custom-field extras.
extra_conn_params = form.data.get("extra")
# The extra value is the combination of custom fields for this conn_type and the Extra field.
# The extra form field with all extra values (including custom fields) is in the form being processed
# so we start with those values, and override them with anything in the custom fields.
extra = {}

extra_field = form.data.get("extra")

if extra_conn_params:
if extra_field:
try:
extra.update(json.loads(extra_conn_params))
extra.update(json.loads(extra_field))
except (JSONDecodeError, TypeError):
flash(
Markup(
Expand All @@ -3615,11 +3613,19 @@ def process_form(self, form, is_created):
"<p>If connection parameters need to be added to <em>Extra</em>, "
"please make sure they are in the form of a single, valid JSON object.</p><br>"
"The following <em>Extra</em> parameters were <b>not</b> added to the connection:<br>"
f"{extra_conn_params}",
f"{extra_field}",
),
category="error",
)

custom_fields = {
key: form.data[key]
for key in self.extra_fields
if key in form.data and key.startswith(f"extra__{conn_type}__")
}

extra.update(custom_fields)

if extra.keys():
form.extra.data = json.dumps(extra)

Expand Down
15 changes: 15 additions & 0 deletions tests/www/views/test_views_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ def test_process_form_extras():

assert json.loads(mock_form.extra.data) == {"extra__test3__custom_field": "custom_field_val3"}

# Testing parameters set in both extra and custom fields (cunnection updates).
mock_form = mock.Mock()
mock_form.data = {
"conn_type": "test4",
"conn_id": "extras_test4",
"extra": '{"extra__test4__custom_field": "custom_field_val3"}',
"extra__test4__custom_field": "custom_field_val4",
}

cmv = ConnectionModelView()
cmv.extra_fields = ["extra__test4__custom_field"] # Custom field
cmv.process_form(form=mock_form, is_created=True)

assert json.loads(mock_form.extra.data) == {"extra__test4__custom_field": "custom_field_val4"}


def test_duplicate_connection(admin_client):
"""Test Duplicate multiple connection with suffix"""
Expand Down