Azure / msrest-for-python

The runtime library "msrest" for AutoRest generated Python clients.
MIT License
41 stars 64 forks source link

Serialize VirtualMachine object exception #19

Closed joe-pll closed 7 years ago

joe-pll commented 7 years ago

Hi,

when serializing a virtual machine object I get an exception, in particular when it tries to serialize the Enum objects such as CachingTypes for the attribute storage_profile of the class azure.mgmt.compute.models.virtual_machine.VirtualMachine.

It tries to serialize using the attribute _attribute_map, not present in the enumeration objects.

def list_all(self):
         try:
             response = self.ComputeManagementClient(<credentials>, <subscription_id>).virtual_machines.list_all()
         except Exception as exc:
             raise Exception(str(exc))
         l = [item for item in response]
         dependencies = dict()
         ...
         serializer = Serializer(classes=dependencies)
         print(serializer.body(l[0], 'VirtualMachine'))

Here the stacktrace i get

Traceback (most recent call last):
...
File \"/usr/lib/python2.7/site-packages/msrest/serialization.py\", line 298, in body
    return self._serialize(data, data_type, **kwargs)
  File \"/usr/lib/python2.7/site-packages/msrest/serialization.py\", line 241, in _serialize
    target_obj, data_type, **kwargs)
  File \"/usr/lib/python2.7/site-packages/msrest/serialization.py\", line 427, in serialize_data
    return self._serialize(data, **kwargs)
  File \"/usr/lib/python2.7/site-packages/msrest/serialization.py\", line 263, in _serialize
    orig_attr, attr_type, **kwargs)
  File \"/usr/lib/python2.7/site-packages/msrest/serialization.py\", line 427, in serialize_data
    return self._serialize(data, **kwargs)
  File \"/usr/lib/python2.7/site-packages/msrest/serialization.py\", line 263, in _serialize
    orig_attr, attr_type, **kwargs)
  File \"/usr/lib/python2.7/site-packages/msrest/serialization.py\", line 427, in serialize_data
    return self._serialize(data, **kwargs)
  File \"/usr/lib/python2.7/site-packages/msrest/serialization.py\", line 263, in _serialize
    orig_attr, attr_type, **kwargs)
  File \"/usr/lib/python2.7/site-packages/msrest/serialization.py\", line 427, in serialize_data
    return self._serialize(data, **kwargs)
  File \"/usr/lib/python2.7/site-packages/msrest/serialization.py\", line 282, in _serialize
    raise_with_traceback(SerializationError, msg, err)
  File \"/usr/lib/python2.7/site-packages/msrest/exceptions.py\", line 50, in raise_with_traceback
    raise error
msrest.exceptions.SerializationError: Attribute None in object CachingTypes cannot be serialized.
CachingTypes.read_write, AttributeError: 'CachingTypes' object has no attribute '_attribute_map'

Am I doing something wrong?

lmazuel commented 7 years ago

Hi @joe-pll I'm not sure of what you are trying to do, but this lib is not intented to be used directly and is not very user-friendly. Could you describe your scenario? You want a dict version of your VM object?

joe-pll commented 7 years ago

Hi @lmazuel, thanks for your reply. I am using this library in order to have a dict version of the VM object. I'll have at the end a list of dict each one representing the VM. For the object NetworkInterface the serialization works like a charm but for the VirtualMachine it doen't. For the moment I implemented my own serialization for the VM but it's not the best solution. What do you suggest?

lmazuel commented 7 years ago

Dict version of a model is now available with to_dict: https://github.com/Azure/msrest-for-python/wiki/msrest-0.4.12---Serialization-change