Skip to content

Commit 1a34a42

Browse files
authored
Update BulkVariableService to support JSON serialization (apache#51057)
1 parent 8bee3fb commit 1a34a42

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

airflow-core/src/airflow/api_fastapi/core_api/services/public/variables.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ def handle_bulk_create(self, action: BulkCreateAction, results: BulkActionRespon
6464

6565
for variable in action.entities:
6666
if variable.key in create_keys:
67+
should_serialize_json = isinstance(variable.value, (dict, list))
6768
Variable.set(
6869
key=variable.key,
6970
value=variable.value,
7071
description=variable.description,
7172
session=self.session,
73+
serialize_json=should_serialize_json,
7274
)
7375
results.success.append(variable.key)
7476

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,55 @@ def test_bulk_variables(self, test_client, actions, expected_results, session):
10601060
assert response_data[key] == value
10611061
check_last_log(session, dag_id=None, event="bulk_variables", logical_date=None)
10621062

1063+
@pytest.mark.parametrize(
1064+
"entity_key, entity_value, entity_description",
1065+
[
1066+
(
1067+
"my_dict_var_param",
1068+
{"name": "Test Dict Param", "id": 123, "active": True},
1069+
"A dict value (param)",
1070+
),
1071+
("my_list_var_param", ["alpha", 42, False, {"nested": "item param"}], "A list value (param)"),
1072+
("my_string_var_param", "plain string param", "A plain string (param)"),
1073+
],
1074+
ids=[
1075+
"dict_variable",
1076+
"list_variable",
1077+
"string_variable",
1078+
],
1079+
)
1080+
def test_bulk_create_entity_serialization(
1081+
self, test_client, session, entity_key, entity_value, entity_description
1082+
):
1083+
actions = {
1084+
"actions": [
1085+
{
1086+
"action": "create",
1087+
"entities": [
1088+
{"key": entity_key, "value": entity_value, "description": entity_description},
1089+
],
1090+
"action_on_existence": "fail",
1091+
}
1092+
]
1093+
}
1094+
1095+
response = test_client.patch("/variables", json=actions)
1096+
assert response.status_code == 200
1097+
1098+
if isinstance(entity_value, (dict, list)):
1099+
retrieved_value_deserialized = Variable.get(entity_key, deserialize_json=True)
1100+
assert retrieved_value_deserialized == entity_value
1101+
retrieved_value_raw_string = Variable.get(entity_key, deserialize_json=False)
1102+
assert retrieved_value_raw_string == json.dumps(entity_value, indent=2)
1103+
else:
1104+
retrieved_value_raw = Variable.get(entity_key, deserialize_json=False)
1105+
assert retrieved_value_raw == str(entity_value)
1106+
1107+
with pytest.raises(json.JSONDecodeError):
1108+
Variable.get(entity_key, deserialize_json=True)
1109+
1110+
check_last_log(session, dag_id=None, event="bulk_variables", logical_date=None)
1111+
10631112
def test_bulk_variables_should_respond_401(self, unauthenticated_test_client):
10641113
response = unauthenticated_test_client.patch("/variables", json={})
10651114
assert response.status_code == 401

0 commit comments

Comments
 (0)