Skip to content

Commit f35ab19

Browse files
guan404mingYour Name
authored andcommitted
[v3-0-test] Update BulkVariableService to support JSON serialization (apache#51057)
(cherry picked from commit 1a34a42) Co-authored-by: Guan Ming(Wesley) Chiu <[email protected]>
1 parent 4ab7b65 commit f35ab19

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

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

tests/api_fastapi/core_api/routes/public/test_variables.py

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

996+
@pytest.mark.parametrize(
997+
"entity_key, entity_value, entity_description",
998+
[
999+
(
1000+
"my_dict_var_param",
1001+
{"name": "Test Dict Param", "id": 123, "active": True},
1002+
"A dict value (param)",
1003+
),
1004+
("my_list_var_param", ["alpha", 42, False, {"nested": "item param"}], "A list value (param)"),
1005+
("my_string_var_param", "plain string param", "A plain string (param)"),
1006+
],
1007+
ids=[
1008+
"dict_variable",
1009+
"list_variable",
1010+
"string_variable",
1011+
],
1012+
)
1013+
def test_bulk_create_entity_serialization(
1014+
self, test_client, session, entity_key, entity_value, entity_description
1015+
):
1016+
actions = {
1017+
"actions": [
1018+
{
1019+
"action": "create",
1020+
"entities": [
1021+
{"key": entity_key, "value": entity_value, "description": entity_description},
1022+
],
1023+
"action_on_existence": "fail",
1024+
}
1025+
]
1026+
}
1027+
1028+
response = test_client.patch("/variables", json=actions)
1029+
assert response.status_code == 200
1030+
1031+
if isinstance(entity_value, (dict, list)):
1032+
retrieved_value_deserialized = Variable.get(entity_key, deserialize_json=True)
1033+
assert retrieved_value_deserialized == entity_value
1034+
retrieved_value_raw_string = Variable.get(entity_key, deserialize_json=False)
1035+
assert retrieved_value_raw_string == json.dumps(entity_value, indent=2)
1036+
else:
1037+
retrieved_value_raw = Variable.get(entity_key, deserialize_json=False)
1038+
assert retrieved_value_raw == str(entity_value)
1039+
1040+
with pytest.raises(json.JSONDecodeError):
1041+
Variable.get(entity_key, deserialize_json=True)
1042+
1043+
check_last_log(session, dag_id=None, event="bulk_variables", logical_date=None)
1044+
9961045
def test_bulk_variables_should_respond_401(self, unauthenticated_test_client):
9971046
response = unauthenticated_test_client.patch("/api/v2/variables", json={})
9981047
assert response.status_code == 401

0 commit comments

Comments
 (0)