This is an automated email from the ASF dual-hosted git repository.

kaxilnaik pushed a commit to branch v3-0-test
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/v3-0-test by this push:
     new e824f7e3ca2 [v3-0-test] Deserialize should work while retrieving 
variables with secrets backend (#50880) (#50889)
e824f7e3ca2 is described below

commit e824f7e3ca244e55166f458aa367674e376a21ac
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Wed May 21 19:55:51 2025 +0530

    [v3-0-test] Deserialize should work while retrieving variables with secrets 
backend (#50880) (#50889)
    
    (cherry picked from commit 6a678060afd1dbc20ddc55aee43e1c9460b7e103)
    
    Co-authored-by: Amogh Desai <[email protected]>
---
 task-sdk/src/airflow/sdk/execution_time/context.py |  4 ++++
 .../tests/task_sdk/definitions/test_variables.py   | 23 ++++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/task-sdk/src/airflow/sdk/execution_time/context.py 
b/task-sdk/src/airflow/sdk/execution_time/context.py
index 9a4f02854e5..dd87bc86771 100644
--- a/task-sdk/src/airflow/sdk/execution_time/context.py
+++ b/task-sdk/src/airflow/sdk/execution_time/context.py
@@ -176,6 +176,10 @@ def _get_variable(key: str, deserialize_json: bool) -> Any:
         try:
             var_val = secrets_backend.get_variable(key=key)  # type: 
ignore[assignment]
             if var_val is not None:
+                if deserialize_json:
+                    import json
+
+                    var_val = json.loads(var_val)
                 return var_val
         except Exception:
             log.exception(
diff --git a/task-sdk/tests/task_sdk/definitions/test_variables.py 
b/task-sdk/tests/task_sdk/definitions/test_variables.py
index 242c5af407b..c9e4698de80 100644
--- a/task-sdk/tests/task_sdk/definitions/test_variables.py
+++ b/task-sdk/tests/task_sdk/definitions/test_variables.py
@@ -109,6 +109,29 @@ class TestVariableFromSecrets:
             assert retrieved_var is not None
             assert retrieved_var == "some_value"
 
+    def test_var_get_from_secrets_found_with_deserialize(self, 
mock_supervisor_comms, tmp_path):
+        """Tests getting a variable from secrets backend when deserialize_json 
is provided."""
+        path = tmp_path / "var.json"
+        dict_data = {"num1": 23, "num2": 42}
+        jsonified_dict_data = json.dumps(dict_data)
+        data = {"VAR_A": jsonified_dict_data}
+        path.write_text(json.dumps(data, indent=4))
+
+        with conf_vars(
+            {
+                (
+                    "workers",
+                    "secrets_backend",
+                ): "airflow.secrets.local_filesystem.LocalFilesystemBackend",
+                ("workers", "secrets_backend_kwargs"): 
f'{{"variables_file_path": "{path}"}}',
+            }
+        ):
+            retrieved_var = Variable.get(key="VAR_A")
+            assert retrieved_var == jsonified_dict_data
+
+            retrieved_var_deser = Variable.get(key="VAR_A", 
deserialize_json=True)
+            assert retrieved_var_deser == dict_data
+
     
@mock.patch("airflow.secrets.environment_variables.EnvironmentVariablesBackend.get_variable")
     def test_get_variable_env_var(self, mock_env_get, mock_supervisor_comms):
         """Tests getting a variable from environment variable."""

Reply via email to