Repository: Azure/msrest-for-python

Issue:
Serialization issue if dict syntax and Duration used
```python
msrest.exceptions.SerializationError: Unable to build a model: Unable to deserialize response data. Data: 3 years, 6 months, 4 days, 12:30:05, duration, TypeError: Expecting a string isodate.duration.Duration(4, 45005, 0, years=3, months=6), DeserializationError: Unable to deserialize response data. Data: 3 years, 6 months, 4 days, 12:30:05, duration, TypeError: Expecting a string isodate.duration.Duration(4, 45005, 0, years=3, months=6)
```

Regression introduced in 0.4.12


Discussion:

Patch:
diff --git a/msrest/serialization.py b/msrest/serialization.py
index 6eb8ec9..063f2e6 100644
--- a/msrest/serialization.py
+++ b/msrest/serialization.py
@@ -918,6 +918,9 @@ class Deserializer(object):
             '[]': self.deserialize_iter,
             '{}': self.deserialize_dict
             }
+        self.deserialize_expected_types = {
+            'duration': (isodate.Duration, datetime.timedelta)
+        }
         self.dependencies = dict(classes) if classes else {}
         self.key_extractors = [
             rest_key_extractor
@@ -1080,6 +1083,8 @@ class Deserializer(object):
             if data_type in self.basic_types.values():
                 return self.deserialize_basic(data, data_type)
             if data_type in self.deserialize_type:
+                if isinstance(data, self.deserialize_expected_types.get(data_type, tuple())):
+                    return data
                 data_val = self.deserialize_type[data_type](data)
                 return data_val
 


Test patch:
diff --git a/tests/test_serialization.py b/tests/test_serialization.py
index f70dcbd..787a086 100644
--- a/tests/test_serialization.py
+++ b/tests/test_serialization.py
@@ -138,7 +138,6 @@ class TestRuntimeSerialized(unittest.TestCase):
 
     class TestObj(Model):
 
-        _validation = {}
         _attribute_map = {
             'attr_a': {'key':'id', 'type':'str'},
             'attr_b': {'key':'AttrB', 'type':'int'},
@@ -147,23 +146,30 @@ class TestRuntimeSerialized(unittest.TestCase):
             'attr_e': {'key':'AttrE', 'type': '{float}'},
             'attr_f': {'key':'AttrF', 'type': 'duration'},
             'attr_g': {'key':'properties.AttrG', 'type':'str'},
-            }
-
-        def __init__(self):
+        }
 
-            self.attr_a = None
-            self.attr_b = None
-            self.attr_c = None
-            self.attr_d = None
-            self.attr_e = None
-            self.attr_f = None
-            self.attr_g = None
+        def __init__(self,
+                attr_a=None,
+                attr_b=None,
+                attr_c=None,
+                attr_d=None,
+                attr_e=None,
+                attr_f=None,
+                attr_g=None):
+
+            self.attr_a = attr_a
+            self.attr_b = attr_b
+            self.attr_c = attr_c
+            self.attr_d = attr_d
+            self.attr_e = attr_e
+            self.attr_f = attr_f
+            self.attr_g = attr_g
 
         def __str__(self):
             return "Test_Object"
 
     def setUp(self):
-        self.s = Serializer()
+        self.s = Serializer({'TestObj': self.TestObj})
         return super(TestRuntimeSerialized, self).setUp()
 
     def test_serialize_direct_model(self):
@@ -496,6 +502,14 @@ class TestRuntimeSerialized(unittest.TestCase):
         message = self.s._serialize(test_obj)
         self.assertEquals("P1D", message["AttrF"])
 
+        test_obj = self.TestObj()
+        test_obj.attr_f = isodate.parse_duration("P3Y6M4DT12H30M5S")
+
+        message = self.s.body({
+            "attr_f": isodate.parse_duration("P3Y6M4DT12H30M5S")},
+            'TestObj')
+        self.assertEquals("P3Y6M4DT12H30M5S", message["AttrF"])
+
     def test_attr_list_simple(self):
         """
         Test serializing an object with simple-typed list attributes
@@ -657,8 +671,8 @@ class TestRuntimeSerialized(unittest.TestCase):
         g = self.s.body({"test":{"value":"data"}}, 'object')
         self.assertEqual(g, {"test":{"value":"data"}})
 
-        h = self.s.serialize_data({"test":self.TestObj()}, 'object')
-        self.assertEqual(h, {"test":"Test_Object"})
+        h = self.s.serialize_data({"test":self.TestObj('id')}, 'object')
+        self.assertEqual(h, {"test":{'id': 'id'}})
 
         i =  self.s.serialize_data({"test":[1,2,3,4,5]}, 'object')
         self.assertEqual(i, {"test":[1,2,3,4,5]})