Skip to content

add failsafe_deserialize #232

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

Merged
merged 4 commits into from
Jan 25, 2021
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
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ To install:
Release History
---------------

2021-01-22 Version 0.6.20
+++++++++++++++++++++++++

**Features**

- Add `failsafe_deserialize` method to the `Deserializer` object. When called, ignores any deserialization errors thrown,
and returns `None`. Recommended when deserializing error models, as in the case of an incorrect error model, we still want
to return the `HttpResponseError` to the user (without a `model`), instead of throwing a `DeserializationError`. #232

2020-09-08 Version 0.6.19
+++++++++++++++++++++++++

Expand Down
20 changes: 20 additions & 0 deletions msrest/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,26 @@ 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):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made the signature the same as __call__, since I wanted calling failsafe_deserialize the same as calling deserializer()

"""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
HttpResponseError to users, and not have them deal with
a deserialization error.

:param str target_obj: The target object type to deserialize to.
:param str/dict data: The response data to deseralize.
:param str content_type: Swagger "produces" if available.
"""
try:
return self(target_obj, data, content_type=content_type)
except:
_LOGGER.warning(
"Ran into a deserialization error. Ignoring since this is failsafe deserialization",
exc_info=True
)
return None

@staticmethod
def _unpack_content(raw_data, content_type=None):
"""Extract the correct structure for deserialization.
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.19"
msrest_version = "0.6.20"
13 changes: 13 additions & 0 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2533,6 +2533,19 @@ class TestModel(Model):
m = TestModel.deserialize({'data': {'id': long_type(1)}})
assert m.data['id'] == long_type(1)

def test_failsafe_deserialization(self):
class Error(Model):

def __init__(self, **kwargs):
self.status = kwargs.pop("status")
self.message = kwargs.pop("message")

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

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

class TestModelInstanceEquality(unittest.TestCase):

def test_model_instance_equality(self):
Expand Down