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
7 changes: 7 additions & 0 deletions msrest/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,9 @@ def _serialize(self, target_obj, data_type=None, **kwargs):
xml_name = "{{{}}}{}".format(xml_ns, xml_name)
serialized.set(xml_name, new_attr)
continue
if xml_desc.get("text", False):
serialized.text = new_attr
continue
if isinstance(new_attr, list):
serialized.extend(new_attr)
elif isinstance(new_attr, ET.Element):
Expand Down Expand Up @@ -1247,6 +1250,10 @@ def xml_key_extractor(attr, attr_desc, data):
if xml_desc.get("attr", False):
return data.get(xml_name)

# If it's x-ms-text, that's simple too
if xml_desc.get("text", False):
return data.text

# Scenario where I take the local name:
# - Wrapped node
# - Internal type is an enum (considered basic types)
Expand Down
45 changes: 45 additions & 0 deletions tests/test_xml_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ class XmlModel(Model):

assert result.language == u"français"

def test_basic_text(self):
"""Test a XML with unicode."""
basic_xml = u"""<?xml version="1.0" encoding="utf-8"?>
<Data language="english">I am text</Data>"""

class XmlModel(Model):
_attribute_map = {
'language': {'key': 'language', 'type': 'str', 'xml':{'name': 'language', 'attr': True}},
'content': {'key': 'content', 'type': 'str', 'xml':{'text': True}},
}
_xml_map = {
'name': 'Data'
}

s = Deserializer({"XmlModel": XmlModel})
result = s(XmlModel, basic_xml, "application/xml")

assert result.language == "english"
assert result.content == "I am text"

def test_add_prop(self):
"""Test addProp as a dict.
"""
Expand Down Expand Up @@ -753,6 +773,31 @@ class XmlModel(Model):

assert_xml_equals(rawxml, basic_xml)

def test_basic_text(self):
"""Test a XML with unicode."""
basic_xml = ET.fromstring("""<?xml version="1.0" encoding="utf-8"?>
<Data language="english">I am text</Data>""")

class XmlModel(Model):
_attribute_map = {
'language': {'key': 'language', 'type': 'str', 'xml':{'name': 'language', 'attr': True}},
'content': {'key': 'content', 'type': 'str', 'xml':{'text': True}},
}
_xml_map = {
'name': 'Data'
}

mymodel = XmlModel(
language="english",
content="I am text"
)

s = Serializer({"XmlModel": XmlModel})
rawxml = s.body(mymodel, 'XmlModel')

assert_xml_equals(rawxml, basic_xml)


def test_direct_array(self):
"""Test an ultra basic XML."""
basic_xml = ET.fromstring("""<?xml version="1.0"?>
Expand Down