From 619d7cdc526c710aa892e1240b61908beb404583 Mon Sep 17 00:00:00 2001 From: Laurent Mazuel Date: Fri, 17 Jul 2020 14:56:59 -0700 Subject: [PATCH] Add support for x-ms-text --- msrest/serialization.py | 7 +++++ tests/test_xml_serialization.py | 45 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/msrest/serialization.py b/msrest/serialization.py index 522ce470fc..34f6e3487b 100644 --- a/msrest/serialization.py +++ b/msrest/serialization.py @@ -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): @@ -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) diff --git a/tests/test_xml_serialization.py b/tests/test_xml_serialization.py index 96229f37a8..9a617f8bf5 100644 --- a/tests/test_xml_serialization.py +++ b/tests/test_xml_serialization.py @@ -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""" + I am text""" + + 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. """ @@ -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(""" + I am text""") + + 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("""