Skip to content

Commit

Permalink
Issue: #99 dicttoxml igores the root param (#100)
Browse files Browse the repository at this point in the history
* Issue: #99 dicttoxml igores the root param

* fix all tests used root=False while also setting wrapper

* explicit coverage of json2xml without root, even if invalid xml

Co-authored-by: Bret Curtis <[email protected]>
  • Loading branch information
psi29a and psi29a authored Jan 29, 2022
1 parent aeb0a3d commit fb8e2f0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
14 changes: 10 additions & 4 deletions json2xml/dicttoxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,14 @@ def dicttoxml(
% (type(obj).__name__, str(obj))
)
output = []
output.append('<?xml version="1.0" encoding="UTF-8" ?>')
output.append(
f"<{custom_root}>{convert(obj, ids, attr_type, item_func, cdata, item_wrap, parent=custom_root)}</{custom_root}>"
)
if root:
output.append('<?xml version="1.0" encoding="UTF-8" ?>')
output.append('<%s>%s</%s>' % (
custom_root,
convert(obj, ids, attr_type, item_func, cdata, item_wrap, parent=custom_root),
custom_root
))
else:
output.append(convert(obj, ids, attr_type, item_func, cdata, item_wrap, parent=''))

return "".join(output).encode("utf-8")
39 changes: 27 additions & 12 deletions tests/test_json2xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

import unittest
from collections import OrderedDict
from pyexpat import ExpatError

import pytest
import xmltodict
import json

from json2xml import json2xml
from json2xml.dicttoxml import dicttoxml
from json2xml.utils import readfromjson, readfromstring, readfromurl, JSONReadError, StringReadError, URLReadError


Expand Down Expand Up @@ -73,29 +76,26 @@ def test_custom_wrapper_and_indent(self):
data = readfromstring(
'{"login":"mojombo","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}'
)
xmldata = json2xml.Json2xml(data, root=False, wrapper="test", pretty=False).to_xml()
xmldata = json2xml.Json2xml(data, wrapper="test", pretty=False).to_xml()
old_dict = xmltodict.parse(xmldata)
# test must be present, snce it is the wrpper
assert "test" in old_dict.keys()
# reverse test, say a wrapper called ramdom won't be present
assert "random" not in old_dict.keys()

def test_no_wrapper_and_indent(self):
def test_no_wrapper(self):
data = readfromstring(
'{"login":"mojombo","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"}'
)
xmldata = json2xml.Json2xml(data, root=False, wrapper="test", pretty=False).to_xml()
old_dict = xmltodict.parse(xmldata)
# test must be present, since it is the wrpper
assert "test" in old_dict.keys()
# reverse test, say a wrapper called ramdom won't be present
assert "random" not in old_dict.keys()
xmldata = json2xml.Json2xml(data, root=False, pretty=False).to_xml()
assert xmldata.startswith(b'<login type="str">mojombo</login>')
self.assertRaises(ExpatError, xmltodict.parse, xmldata)

def test_item_wrap(self):
data = readfromstring(
'{"my_items":[{"my_item":{"id":1} },{"my_item":{"id":2} }],"my_str_items":["a","b"]}'
)
xmldata = json2xml.Json2xml(data, root=False, pretty=False).to_xml()
xmldata = json2xml.Json2xml(data, pretty=False).to_xml()
old_dict = xmltodict.parse(xmldata)
# item must be present within my_items
print(xmldata)
Expand All @@ -106,7 +106,7 @@ def test_no_item_wrap(self):
data = readfromstring(
'{"my_items":[{"my_item":{"id":1} },{"my_item":{"id":2} }],"my_str_items":["a","b"]}'
)
xmldata = json2xml.Json2xml(data, root=False, pretty=False, item_wrap=False).to_xml()
xmldata = json2xml.Json2xml(data, pretty=False, item_wrap=False).to_xml()
old_dict = xmltodict.parse(xmldata)
# my_item must be present within my_items
print(xmldata)
Expand All @@ -117,7 +117,7 @@ def test_empty_array(self):
data = readfromstring(
'{"empty_list":[]}'
)
xmldata = json2xml.Json2xml(data, root=False, pretty=False).to_xml()
xmldata = json2xml.Json2xml(data, pretty=False).to_xml()
old_dict = xmltodict.parse(xmldata)
print(xmldata)
# item empty_list be present within all
Expand All @@ -127,7 +127,7 @@ def test_attrs(self):
data = readfromstring(
'{"my_string":"a","my_int":1,"my_float":1.1,"my_bool":true,"my_null":null,"empty_list":[],"empty_dict":{}}'
)
xmldata = json2xml.Json2xml(data, root=False, pretty=False).to_xml()
xmldata = json2xml.Json2xml(data, pretty=False).to_xml()
old_dict = xmltodict.parse(xmldata)
print(xmldata)
# test all attrs
Expand All @@ -149,3 +149,18 @@ def test_dicttoxml_bug(self):
xmldata = json2xml.Json2xml(json.dumps(input_dict), wrapper='response', pretty=False, attr_type=False, item_wrap=False).to_xml()
old_dict = xmltodict.parse(xmldata)
assert 'response' in old_dict.keys()

def test_dict2xml_no_root(self):
payload = {'mock': 'payload'}
result = dicttoxml(payload, attr_type=False, root=False)
assert b'<mock>payload</mock>' == result

def test_dict2xml_with_root(self):
payload = {'mock': 'payload'}
result = dicttoxml(payload, attr_type=False)
assert b'<?xml version="1.0" encoding="UTF-8" ?><root><mock>payload</mock></root>' == result

def test_dict2xml_with_custom_root(self):
payload = {'mock': 'payload'}
result = dicttoxml(payload, attr_type=False, custom_root="element")
assert b'<?xml version="1.0" encoding="UTF-8" ?><element><mock>payload</mock></element>' == result

0 comments on commit fb8e2f0

Please sign in to comment.