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

[master] Porting #51322 to master #54597

Merged
merged 7 commits into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 10 additions & 4 deletions salt/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,21 @@ def ext_type_encoder(obj):
except (OverflowError, msgpack.exceptions.PackValueError):
# msgpack<=0.4.6 don't call ext encoder on very long integers raising the error instead.
# Convert any very long longs to strings and call dumps again.
def verylong_encoder(obj):
def verylong_encoder(obj, context):
# Make sure we catch recursion here.
objid = id(obj)
if objid in context:
return '<Recursion on {} with id={}>'.format(type(obj).__name__, id(obj))
context.add(objid)

if isinstance(obj, dict):
for key, value in six.iteritems(obj.copy()):
obj[key] = verylong_encoder(value)
obj[key] = verylong_encoder(value, context)
return dict(obj)
elif isinstance(obj, (list, tuple)):
obj = list(obj)
for idx, entry in enumerate(obj):
obj[idx] = verylong_encoder(entry)
obj[idx] = verylong_encoder(entry, context)
return obj
# A value of an Integer object is limited from -(2^63) upto (2^64)-1 by MessagePack
# spec. Here we care only of JIDs that are positive integers.
Expand All @@ -260,7 +266,7 @@ def verylong_encoder(obj):
else:
return obj

msg = verylong_encoder(msg)
msg = verylong_encoder(msg, set())
if msgpack.version >= (0, 4, 0):
return msgpack.dumps(msg, default=ext_type_encoder, use_bin_type=use_bin_type)
else:
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ def test_mixed_dump_load(self):
odata = payload.loads(sdata)
self.assertEqual(edata, odata)

def test_recursive_dump_load(self):
'''
Test recursive payloads are (mostly) serialized
'''
payload = salt.payload.Serial('msgpack')
data = {'name': 'roscivs'}
data['data'] = data # Data all the things!
sdata = payload.dumps(data)
odata = payload.loads(sdata)
self.assertTrue('recursion' in odata['data'].lower())


class SREQTestCase(TestCase):
port = 8845 # TODO: dynamically assign a port?
Expand Down