Skip to content

Commit 1c99be9

Browse files
amoghrajeshdadonnelly316
authored andcommitted
Deserialize should work while retrieving variables with secrets backend (apache#50880)
1 parent 9d8e5f5 commit 1c99be9

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

task-sdk/src/airflow/sdk/execution_time/context.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ def _get_variable(key: str, deserialize_json: bool) -> Any:
176176
try:
177177
var_val = secrets_backend.get_variable(key=key) # type: ignore[assignment]
178178
if var_val is not None:
179+
if deserialize_json:
180+
import json
181+
182+
var_val = json.loads(var_val)
179183
return var_val
180184
except Exception:
181185
log.exception(

task-sdk/tests/task_sdk/definitions/test_variables.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@ def test_var_get_from_secrets_found(self, mock_supervisor_comms, tmp_path):
109109
assert retrieved_var is not None
110110
assert retrieved_var == "some_value"
111111

112+
def test_var_get_from_secrets_found_with_deserialize(self, mock_supervisor_comms, tmp_path):
113+
"""Tests getting a variable from secrets backend when deserialize_json is provided."""
114+
path = tmp_path / "var.json"
115+
dict_data = {"num1": 23, "num2": 42}
116+
jsonified_dict_data = json.dumps(dict_data)
117+
data = {"VAR_A": jsonified_dict_data}
118+
path.write_text(json.dumps(data, indent=4))
119+
120+
with conf_vars(
121+
{
122+
(
123+
"workers",
124+
"secrets_backend",
125+
): "airflow.secrets.local_filesystem.LocalFilesystemBackend",
126+
("workers", "secrets_backend_kwargs"): f'{{"variables_file_path": "{path}"}}',
127+
}
128+
):
129+
retrieved_var = Variable.get(key="VAR_A")
130+
assert retrieved_var == jsonified_dict_data
131+
132+
retrieved_var_deser = Variable.get(key="VAR_A", deserialize_json=True)
133+
assert retrieved_var_deser == dict_data
134+
112135
@mock.patch("airflow.secrets.environment_variables.EnvironmentVariablesBackend.get_variable")
113136
def test_get_variable_env_var(self, mock_env_get, mock_supervisor_comms):
114137
"""Tests getting a variable from environment variable."""

0 commit comments

Comments
 (0)