Skip to content

Commit c13f5fb

Browse files
amoghrajeshYour Name
authored and
Your Name
committed
[v3-0-test] Deserialize should work while retrieving variables with secrets backend (#50880)
(cherry picked from commit 6a67806) Co-authored-by: Amogh Desai <amoghrajesh1999@gmail.com>
1 parent 38225c6 commit c13f5fb

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy