Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode old-style nested Xcom value #31866

Merged
merged 16 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion airflow/serialization/serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
OLD_TYPE = "__type"
OLD_SOURCE = "__source"
OLD_DATA = "__var"
OLD_DICT = "dict"

DEFAULT_VERSION = 0

Expand Down Expand Up @@ -276,7 +277,11 @@ def deserialize(o: T | None, full=True, type_hint: Any = None) -> object:
def _convert(old: dict) -> dict:
"""Converts an old style serialization to new style."""
if OLD_TYPE in old and OLD_DATA in old:
return {CLASSNAME: old[OLD_TYPE], VERSION: DEFAULT_VERSION, DATA: old[OLD_DATA][OLD_DATA]}
# Return old style dicts directly as they do not need wrapping
if old[OLD_TYPE] == OLD_DICT:
return old[OLD_DATA]
else:
return {CLASSNAME: old[OLD_TYPE], VERSION: DEFAULT_VERSION, DATA: old[OLD_DATA]}
utkarsharma2 marked this conversation as resolved.
Show resolved Hide resolved

return old

Expand Down
19 changes: 18 additions & 1 deletion tests/serialization/test_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,21 +257,38 @@ def test_raise_undeserializable(self):
deserialize(data)

def test_backwards_compat(self):
"""
Verify deserialization of old-style encoded Xcom values including nested ones
"""
uri = "s3://does_not_exist"
data = {
"__type": "airflow.datasets.Dataset",
"__source": None,
"__var": {
"__var": {
"uri": uri,
"extra": None,
"extra": {
"__var": {"hi": "bye"},
"__type": "dict",
},
},
"__type": "dict",
},
}
dataset = deserialize(data)
assert dataset.extra == {"hi": "bye"}
assert dataset.uri == uri

def test_backwards_compat_wrapped(self):
"""
Verify deserialization of old-style wrapped XCom value
"""
i = {
"extra": {"__var": {"hi": "bye"}, "__type": "dict"},
}
e = deserialize(i)
assert e["extra"] == {"hi": "bye"}

def test_encode_dataset(self):
dataset = Dataset("mytest://dataset")
obj = deserialize(serialize(dataset))
Expand Down