Skip to content

Commit

Permalink
fix failsafe deserialization (#234)
Browse files Browse the repository at this point in the history
* fix failsafe deserialization

* add extra checks

* fix changelog
  • Loading branch information
iscai-msft authored Jan 26, 2021
1 parent a554c11 commit 9b67dea
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ To install:
Release History
---------------

2021-01-26 Version 0.6.21
+++++++++++++++++++++++++

**Bug Fixes**

- Fixes `failsafe_deserialize` introduced in `0.6.20` #232

2021-01-25 Version 0.6.20
+++++++++++++++++++++++++

Expand Down
2 changes: 1 addition & 1 deletion msrest/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ def _classify_target(self, target, data):
pass # Target is not a Model, no classify
return target, target.__class__.__name__

def failsafe_deserialize(self, target_obj, response_data, content_type=None):
def failsafe_deserialize(self, target_obj, data, content_type=None):
"""Ignores any errors encountered in deserialization,
and falls back to not deserializing the object. Recommended
for use in error deserialization, as we want to return the
Expand Down
2 changes: 1 addition & 1 deletion msrest/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
# --------------------------------------------------------------------------

#: version of this package. Use msrest.__version__ instead
msrest_version = "0.6.20"
msrest_version = "0.6.21"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

setup(
name='msrest',
version='0.6.20',
version='0.6.21',
author='Microsoft Corporation',
packages=find_packages(exclude=["tests", "tests.*"]),
url=("https://github.com/Azure/msrest-for-python"),
Expand Down
18 changes: 16 additions & 2 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2536,16 +2536,30 @@ class TestModel(Model):
def test_failsafe_deserialization(self):
class Error(Model):

_attribute_map = {
"status": {"key": "status", "type": "int"},
"message": {"key": "message", "type": "str"},
}

def __init__(self, **kwargs):
self.status = kwargs.pop("status")
self.message = kwargs.pop("message")
super(Error, self).__init__(**kwargs)
self.status = kwargs.get("status", None)
self.message = kwargs.get("message", None)


with pytest.raises(DeserializationError):
self.d(Error, json.dumps(''), 'text/html')

# should fail
deserialized = self.d.failsafe_deserialize(Error, json.dumps(''), 'text/html')
assert deserialized is None

# should not fail
error = {"status": 400, "message": "should deserialize"}
deserialized = self.d.failsafe_deserialize(Error, json.dumps(error), 'application/json')
assert deserialized.status == 400
assert deserialized.message == "should deserialize"

class TestModelInstanceEquality(unittest.TestCase):

def test_model_instance_equality(self):
Expand Down

0 comments on commit 9b67dea

Please sign in to comment.