Skip to content

Commit

Permalink
Merge pull request #2241 from SFDO-Tooling/feature/namespace-improvem…
Browse files Browse the repository at this point in the history
…ents

tostring() works for individual elements and parent namespaces can be printed
  • Loading branch information
prescod committed Dec 9, 2020
2 parents 512dae3 + d6c732f commit fbe4a23
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
8 changes: 6 additions & 2 deletions cumulusci/utils/xml/metadata_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,17 @@ def matches(e):
if matches(e)
)

def tostring(self, xml_declaration=False):
def tostring(self, xml_declaration=False, include_parent_namespaces=False):
"""Serialize back to XML.
The XML Declaration is optional and can be controlled by `xml_declaration`"""
doc = etree.ElementTree(self._element)
etree.indent(doc, space=" ")
return serialize_xml_for_salesforce(doc, xml_declaration=xml_declaration)
return serialize_xml_for_salesforce(
doc,
xml_declaration=xml_declaration,
include_parent_namespaces=include_parent_namespaces,
)

def __eq__(self, other: "MetadataElement"):
eq = self._element == other._element
Expand Down
15 changes: 10 additions & 5 deletions cumulusci/utils/xml/salesforce_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
_supported_events = ("start", "end", "start-ns", "end-ns", "comment")


def serialize_xml_for_salesforce(xdoc, xml_declaration=True):
def serialize_xml_for_salesforce(
xdoc, xml_declaration=True, include_parent_namespaces=False
):
r = xml_encoding if xml_declaration else ""

new_namespace_declarations = {}
all_namespaces = {}

if include_parent_namespaces:
new_namespace_declarations = {
prefix: url for prefix, url in xdoc.getroot().nsmap.items()
}
else:
new_namespace_declarations = {}
all_namespaces = {url: prefix for prefix, url in xdoc.getroot().nsmap.items()}
for action, elem in etree.iterwalk(xdoc, events=_supported_events):
if action == "start-ns":
prefix, ns = elem
Expand Down
18 changes: 18 additions & 0 deletions cumulusci/utils/xml/test/test_metadata_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,21 @@ def test_multiple_namespaces(self):
</CustomMetadata>""".strip()
CustomMetadata = fromstring(xml)
assert xml.strip() == CustomMetadata.tostring().strip()

def test_namespaced_to_string__do_not_output_namespaces(self):
CustomMetadata = fromstring(standard_xml)

xml_out = CustomMetadata.find("bar").find("name").tostring()
assert xml_out.strip() == "<name>Bar1</name>", xml_out.strip()

def test_namespaced_to_string__output_namespaces(self):
CustomMetadata = fromstring(standard_xml)
xml_out = (
CustomMetadata.find("bar")
.find("name")
.tostring(xml_declaration=True, include_parent_namespaces=True)
)
expected_out = """<?xml version="1.0" encoding="UTF-8"?> <name xmlns="http://soap.sforce.com/2006/04/metadata">Bar1</name>"""
assert (
" ".join(xml_out.split()).strip() == " ".join(expected_out.split()).strip()
), xml_out.strip()

0 comments on commit fbe4a23

Please sign in to comment.