Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove #text element from XML responses #2079

Merged
merged 3 commits into from
May 18, 2021
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
27 changes: 27 additions & 0 deletions onadata/apps/api/tests/viewsets/test_data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2565,6 +2565,33 @@ def test_data_list_xml_format(self):
'</submission-item></submission-batch>')
self.assertEqual(expected_xml, returned_xml)

def test_invalid_xml_elements_not_present(self):
"""
Test invalid XML elements such as #text are not present in XML Response
"""
self._make_submission(os.path.join(
self.this_directory, 'fixtures',
'transportation', 'instances', 'transport_2011-07-25_19-05-49_2',
'transport_2011-07-25_19-05-49_2.xml'))
media_file = "1335783522563.jpg"
self._make_submission_w_attachment(os.path.join(
self.this_directory, 'fixtures',
'transportation', 'instances', 'transport_2011-07-25_19-05-49_2',
'transport_2011-07-25_19-05-49_2.xml'),
os.path.join(self.this_directory, 'fixtures',
'transportation', 'instances',
'transport_2011-07-25_19-05-49_2', media_file))

view = DataViewSet.as_view({'get': 'list'})
request = self.factory.get('/', **self.extra)
response = view(request, pk=self.xform.pk, format='xml')
self.assertEqual(response.status_code, 200)

# Ensure XML is well formed
response.render()
returned_xml = response.content.decode('utf-8')
ET.fromstring(returned_xml)

@override_settings(STREAM_DATA=True)
def test_data_list_xml_format_no_data(self):
"""Test DataViewSet Query list XML"""
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version='1.0' ?><transportation id="transportation_2011_07_25" version="2014111"><transport><available_transportation_types_to_referral_facility>none</available_transportation_types_to_referral_facility><loop_over_transport_types_frequency><ambulance /><bicycle /><boat_canoe /><bus /><donkey_mule_cart /><keke_pepe /><lorry /><motorbike /><taxi /><other /></loop_over_transport_types_frequency></transport><image1 type="file">1335783522563.jpg</image1><meta><instanceID>uuid:5b2cc313-fc09-437e-8149-fcd32f695d41</instanceID></meta></transportation>
5 changes: 3 additions & 2 deletions onadata/libs/renderers/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,9 @@ def _to_xml(self, xml, data):

elif isinstance(data, dict):
for key, value in data.items():
if isinstance(value, (list, tuple)):
if not key:
self._to_xml(xml, value)
elif isinstance(value, (list, tuple)):
for v in value:
xml.startElement(key, {})
self._to_xml(xml, v)
Expand All @@ -419,7 +421,6 @@ def _to_xml(self, xml, data):
xml.startElement(key, attributes)
self._to_xml(xml, value)
xml.endElement(key)

else:
xml.startElement(key, {})
self._to_xml(xml, value)
Expand Down
2 changes: 1 addition & 1 deletion onadata/libs/serializers/data_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def to_representation(self, instance):
ret = super(
DataInstanceXMLSerializer, self).to_representation(instance)
if 'xml' in ret:
ret = xmltodict.parse(ret['xml'])
ret = xmltodict.parse(ret['xml'], cdata_key="")

# Add Instance attributes to representation
instance_attributes = {
Expand Down