Skip to content

msrest.serialization serializes CloudEvent data field object as string #12831

@t-swpill

Description

@t-swpill

I am trying to send a CloudEvent to an Event Grid topic, where CloudEvent is a model which inherits from msrest.serialization.Model. The CloudEvent is the following:

event = CloudEvent(
                type="Azure.Sdk.Sample",
                source="https://egsample.dev/sampleevent",
                data=CustomSample("custom event")
                )

The CustomSample class is:

class CustomSample(msrest.serialization.Model):
    _validation = {
            'a': {'required': True},
    }

    _attribute_map = {
        'a': {'key': 'a', 'type': 'str'},
    }

    def __init__(self, a):
        self.a = a

When the CloudEvent is serialized to send to the Event Grid service, the CustomSample object in the data field is serialized into a string. This means that, when I try to deserialize the CloudEvent again on the receiving side, the data is not deserialized into the original CustomSample object again.

For example, this code:

serialized_event = CloudEvent.serialize(event)
print("serialized: {}".format(serialized_event))
deserialized_event = CloudEvent.deserialize(serialized_event)
print("deserialized: {}".format(deserialized_event))

results in this output:

serialized: {'id': '73cdccf6-e4b0-485f-8965-d27bad36bedc', 'source': 'https://egsample.dev/sampleevent', 'data': "{'a': 'sample event'}", 'type': 'Azure.Sdk.Sample', 'time': '2020-07-30T19:19:39.165385Z', 'specversion': '1.0'}
deserialized: {'additional_properties': None, 'id': '73cdccf6-e4b0-485f-8965-d27bad36bedc', 'data': "{'a': 'sample event'}", 'data_base64': None, 'time': datetime.datetime(2020, 7, 30, 
19, 19, 39, 165385, tzinfo=<isodate.tzinfo.Utc object at 0x000001FF9C64E400>), 'specversion': '1.0', 'dataschema': None, 'datacontenttype': None, 'subject': None, 'source': 'https://egsample.dev/sampleevent', 'type': 'Azure.Sdk.Sample'}

When the value of the data field is set to another type, such as an int or dict, it is serialized/deserialized as an int or dict, respectively. I would similarly expect the data field to be serialized/deserialized as a CustomSample object, instead of a string.

Metadata

Metadata

Labels

ClientThis issue points to a problem in the data-plane of the library.Event GridMessagingMessaging crew

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions