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

added functionality to correctly parse xml lists #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Django>=1.6
djangorestframework>=2.4.3
pytest-django==2.6
pytest-django==2.9.1
pytest==2.5.2
pytest-cov==1.6
flake8==2.2.2
9 changes: 8 additions & 1 deletion rest_framework_xml/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ def parse(self, stream, media_type=None, parser_context=None):

return data

def _check_xml_list(self, element):
"""
Checks that an element has multiple tags and that they are all the same,
to validate that the element is a properly formatted list
"""
return len(element) > 1 and len(set([child.tag for child in element])) <= 1

def _xml_convert(self, element):
"""
convert the xml `element` into the corresponding python object
Expand All @@ -48,7 +55,7 @@ def _xml_convert(self, element):
return self._type_convert(element.text)
else:
# if the fist child tag is list-item means all children are list-item
if children[0].tag == "list-item":
if self._check_xml_list(element):
data = []
for child in children:
data.append(self._xml_convert(child))
Expand Down
55 changes: 55 additions & 0 deletions tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,49 @@ def setUp(self):
}
]
}
self._invalid_list_input = StringIO(
'<?xml version="1.0" encoding="utf-8"?>'
'<root>'
'<list>'
'<list-item><sub_id>1</sub_id><sub_name>first</sub_name></list-item>'
'<list-item2><sub_id>2</sub_id><sub_name>second</sub_name></list-item2>'
'<list-item2><sub_id>3</sub_id><sub_name>third</sub_name></list-item2>'
'</list>'
'</root>'
)
self._invalid_list_output = {
"list": {
"list-item": {
"sub_id": 1,
"sub_name": "first"
},
"list-item2": {
"sub_id": 3,
"sub_name": "third"
}
}
}
self._valid_list_input = StringIO(
'<?xml version="1.0" encoding="utf-8"?>'
'<root>'
'<list>'
'<list-item><sub_id>1</sub_id><sub_name>first</sub_name></list-item>'
'<list-item><sub_id>2</sub_id><sub_name>second</sub_name></list-item>'
'</list>'
'</root>'
)
self._valid_list_output = {
"list": [
{
"sub_id": 1,
"sub_name": "first"
},
{
"sub_id": 2,
"sub_name": "second"
}
]
}

@unittest.skipUnless(etree, 'defusedxml not installed')
def test_parse(self):
Expand All @@ -64,3 +107,15 @@ def test_complex_data_parse(self):
parser = XMLParser()
data = parser.parse(self._complex_data_input)
self.assertEqual(data, self._complex_data)

@unittest.skipUnless(etree, 'defusedxml not installed')
def test_invalid_list_parse(self):
parser = XMLParser()
data = parser.parse(self._invalid_list_input)
self.assertEqual(data, self._invalid_list_output)

@unittest.skipUnless(etree, 'defusedxml not installed')
def test_valid_list_parse(self):
parser = XMLParser()
data = parser.parse(self._valid_list_input)
self.assertEqual(data, self._valid_list_output)