From 9b67deacc860e10cd95eb968cfa770fe475f7d8e Mon Sep 17 00:00:00 2001 From: iscai-msft <43154838+iscai-msft@users.noreply.github.com> Date: Tue, 26 Jan 2021 18:45:09 -0500 Subject: [PATCH] fix failsafe deserialization (#234) * fix failsafe deserialization * add extra checks * fix changelog --- README.rst | 7 +++++++ msrest/serialization.py | 2 +- msrest/version.py | 2 +- setup.py | 2 +- tests/test_serialization.py | 18 ++++++++++++++++-- 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 4c199e9cb..1476b4060 100644 --- a/README.rst +++ b/README.rst @@ -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 +++++++++++++++++++++++++ diff --git a/msrest/serialization.py b/msrest/serialization.py index 36587a777..688ffeed3 100644 --- a/msrest/serialization.py +++ b/msrest/serialization.py @@ -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 diff --git a/msrest/version.py b/msrest/version.py index 1d7b6eea0..e24924ac7 100644 --- a/msrest/version.py +++ b/msrest/version.py @@ -25,4 +25,4 @@ # -------------------------------------------------------------------------- #: version of this package. Use msrest.__version__ instead -msrest_version = "0.6.20" +msrest_version = "0.6.21" diff --git a/setup.py b/setup.py index 236295d96..27c113299 100644 --- a/setup.py +++ b/setup.py @@ -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"), diff --git a/tests/test_serialization.py b/tests/test_serialization.py index c57b56127..fa4776a84 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -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):