Skip to content
This repository was archived by the owner on Feb 28, 2026. It is now read-only.
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
11 changes: 9 additions & 2 deletions msrest/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,15 @@ def _classify(cls, response, objects):
for subtype_key in cls.__dict__.get('_subtype_map', {}).keys():
subtype_value = None

rest_api_response_key = cls._get_rest_key_parts(subtype_key)[-1]
subtype_value = response.pop(rest_api_response_key, None) or response.pop(subtype_key, None)
if not isinstance(response, ET.Element):
rest_api_response_key = cls._get_rest_key_parts(subtype_key)[-1]
subtype_value = response.pop(rest_api_response_key, None) or response.pop(subtype_key, None)
else:
subtype_value = xml_key_extractor(
subtype_key,
cls._attribute_map[subtype_key],
response
)
if subtype_value:
# Try to match base class. Can be class name only
# (bug to fix in Autorest to support x-ms-discriminator-name)
Expand Down
66 changes: 66 additions & 0 deletions tests/test_xml_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,72 @@ class MessageCountDetails(Model):
assert result.authorization_rules[0].type == "SharedAccessAuthorizationRule"
assert result.message_count_details.active_message_count == 12

def test_polymorphic_deserialization(self):

basic_xml = """<?xml version="1.0"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Filter xsi:type="CorrelationFilter">
<CorrelationId>12</CorrelationId>
</Filter>
</entry>"""

class XmlRoot(Model):
_attribute_map = {
'filter': {'key': 'Filter', 'type': 'RuleFilter'},
}
_xml_map = {
'name': 'entry'
}

class RuleFilter(Model):
_attribute_map = {
'type': {'key': 'type', 'type': 'str', 'xml': {'attr': True, 'prefix': 'xsi', 'ns': 'http://www.w3.org/2001/XMLSchema-instance'}},
}

_subtype_map = {
'type': {'CorrelationFilter': 'CorrelationFilter', 'SqlFilter': 'SqlFilter'}
}
_xml_map = {
'name': 'Filter'
}

class CorrelationFilter(RuleFilter):
_attribute_map = {
'type': {'key': 'type', 'type': 'str', 'xml': {'attr': True, 'prefix': 'xsi', 'ns': 'http://www.w3.org/2001/XMLSchema-instance'}},
'correlation_id': {'key': 'CorrelationId', 'type': 'int'},
}

def __init__(
self,
correlation_id = None,
**kwargs
):
super(CorrelationFilter, self).__init__(**kwargs)
self.type = 'CorrelationFilter'
self.correlation_id = correlation_id

class SqlFilter(RuleFilter):
_attribute_map = {
'type': {'key': 'type', 'type': 'str', 'xml': {'attr': True, 'prefix': 'xsi', 'ns': 'http://www.w3.org/2001/XMLSchema-instance'}},
}

def __init__(
self,
**kwargs
):
pytest.fail("Don't instantiate me")

s = Deserializer({
"XmlRoot": XmlRoot,
"RuleFilter": RuleFilter,
"SqlFilter": SqlFilter,
"CorrelationFilter": CorrelationFilter,
})
result = s(XmlRoot, basic_xml, "application/xml")

assert isinstance(result.filter, CorrelationFilter)
assert result.filter.correlation_id == 12


class TestXmlSerialization:

Expand Down