airflow-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bo...@apache.org
Subject incubator-airflow git commit: [AIRFLOW-1177] Fix Variable.setdefault w/existing JSON
Date Sat, 16 Sep 2017 15:40:02 GMT
Repository: incubator-airflow
Updated Branches:
  refs/heads/v1-9-test ad963e704 -> 65319a3db


[AIRFLOW-1177] Fix Variable.setdefault w/existing JSON

Previously due to a logic error if you attempt to
use
`Variable.setdefault()` with
`deserialize_json=True` and the value
already existed it would die with:

    ...
        my = Variable.setdefault('regions', ['uk'],
deserialize_json=True)
      File "/usr/local/lib/python3.5/site-
packages/airflow/models.py", line 3623, in
setdefault
        return json.loads(obj.val)
    AttributeError: 'str' object has no attribute
'val'

The problem was that the `Variable.get()` call was
returning the value,
not a variable object.

Closes #2540 from ashb/variable-setdefault-json

(cherry picked from commit 610f68b22a8277f9521d0b9989b4dd5ce7cbcfe0)
Signed-off-by: Bolke de Bruin <bolke@xs4all.nl>


Project: http://git-wip-us.apache.org/repos/asf/incubator-airflow/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-airflow/commit/65319a3d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-airflow/tree/65319a3d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-airflow/diff/65319a3d

Branch: refs/heads/v1-9-test
Commit: 65319a3db90848d4efd409d1dc803a6bc0cae5e0
Parents: ad963e7
Author: Ash Berlin-Taylor <ash_github@firemirror.com>
Authored: Sat Sep 16 17:39:06 2017 +0200
Committer: Bolke de Bruin <bolke@xs4all.nl>
Committed: Sat Sep 16 17:39:52 2017 +0200

----------------------------------------------------------------------
 airflow/models.py | 7 ++-----
 tests/core.py     | 9 +++++++++
 2 files changed, 11 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-airflow/blob/65319a3d/airflow/models.py
----------------------------------------------------------------------
diff --git a/airflow/models.py b/airflow/models.py
index be36559..f690fb4 100755
--- a/airflow/models.py
+++ b/airflow/models.py
@@ -3957,7 +3957,7 @@ class Variable(Base, LoggingMixin):
         :return: Mixed
         """
         default_sentinel = object()
-        obj = Variable.get(key, default_var=default_sentinel, deserialize_json=False)
+        obj = Variable.get(key, default_var=default_sentinel, deserialize_json=deserialize_json)
         if obj is default_sentinel:
             if default is not None:
                 Variable.set(key, default, serialize_json=deserialize_json)
@@ -3965,10 +3965,7 @@ class Variable(Base, LoggingMixin):
             else:
                 raise ValueError('Default Value must be set')
         else:
-            if deserialize_json:
-                return json.loads(obj.val)
-            else:
-                return obj.val
+            return obj
 
     @classmethod
     @provide_session

http://git-wip-us.apache.org/repos/asf/incubator-airflow/blob/65319a3d/tests/core.py
----------------------------------------------------------------------
diff --git a/tests/core.py b/tests/core.py
index acc543b..98e05a0 100644
--- a/tests/core.py
+++ b/tests/core.py
@@ -728,6 +728,15 @@ class CoreTest(unittest.TestCase):
         Variable.setdefault(key, value, deserialize_json=True)
         self.assertEqual(value, Variable.get(key, deserialize_json=True))
 
+    def test_variable_setdefault_existing_json(self):
+        key = "tested_var_setdefault_2_id"
+        value = {"city": 'Paris', "Hapiness": True}
+        Variable.set(key, value, serialize_json=True)
+        val = Variable.setdefault(key, value, deserialize_json=True)
+        # Check the returned value, and the stored value are handled correctly.
+        self.assertEqual(value, val)
+        self.assertEqual(value, Variable.get(key, deserialize_json=True))
+
     def test_parameterized_config_gen(self):
 
         cfg = configuration.parameterized_config(configuration.DEFAULT_CONFIG)


Mime
View raw message