Skip to content

Commit 9b67dea

Browse files
authored
fix failsafe deserialization (#234)
* fix failsafe deserialization * add extra checks * fix changelog
1 parent a554c11 commit 9b67dea

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

README.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ To install:
2020
Release History
2121
---------------
2222

23+
2021-01-26 Version 0.6.21
24+
+++++++++++++++++++++++++
25+
26+
**Bug Fixes**
27+
28+
- Fixes `failsafe_deserialize` introduced in `0.6.20` #232
29+
2330
2021-01-25 Version 0.6.20
2431
+++++++++++++++++++++++++
2532

msrest/serialization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ def _classify_target(self, target, data):
14861486
pass # Target is not a Model, no classify
14871487
return target, target.__class__.__name__
14881488

1489-
def failsafe_deserialize(self, target_obj, response_data, content_type=None):
1489+
def failsafe_deserialize(self, target_obj, data, content_type=None):
14901490
"""Ignores any errors encountered in deserialization,
14911491
and falls back to not deserializing the object. Recommended
14921492
for use in error deserialization, as we want to return the

msrest/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
# --------------------------------------------------------------------------
2626

2727
#: version of this package. Use msrest.__version__ instead
28-
msrest_version = "0.6.20"
28+
msrest_version = "0.6.21"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
setup(
3030
name='msrest',
31-
version='0.6.20',
31+
version='0.6.21',
3232
author='Microsoft Corporation',
3333
packages=find_packages(exclude=["tests", "tests.*"]),
3434
url=("https://github.com/Azure/msrest-for-python"),

tests/test_serialization.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,16 +2536,30 @@ class TestModel(Model):
25362536
def test_failsafe_deserialization(self):
25372537
class Error(Model):
25382538

2539+
_attribute_map = {
2540+
"status": {"key": "status", "type": "int"},
2541+
"message": {"key": "message", "type": "str"},
2542+
}
2543+
25392544
def __init__(self, **kwargs):
2540-
self.status = kwargs.pop("status")
2541-
self.message = kwargs.pop("message")
2545+
super(Error, self).__init__(**kwargs)
2546+
self.status = kwargs.get("status", None)
2547+
self.message = kwargs.get("message", None)
2548+
25422549

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

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

2557+
# should not fail
2558+
error = {"status": 400, "message": "should deserialize"}
2559+
deserialized = self.d.failsafe_deserialize(Error, json.dumps(error), 'application/json')
2560+
assert deserialized.status == 400
2561+
assert deserialized.message == "should deserialize"
2562+
25492563
class TestModelInstanceEquality(unittest.TestCase):
25502564

25512565
def test_model_instance_equality(self):

0 commit comments

Comments
 (0)