Skip to content

Commit

Permalink
🐛 Fix converting dict having integer keys
Browse files Browse the repository at this point in the history
Taken from quandyfactory#62
  • Loading branch information
Ousret committed Aug 13, 2022
1 parent 5e4d309 commit 70689d7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dicttoxml2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def make_valid_xml_name(key: str, attr: Dict[str, str]):
return key, attr

# prepend a lowercase n if the key is numeric
if key.isdigit():
if isinstance(key, int) or key.isdigit():
return 'n%s' % (key), attr

# replace spaces with underscores if that fixes the problem
Expand Down
6 changes: 6 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,9 @@ def test_folding_list_ignored() -> None:
payload: dict = {'book': [{'title': 'Python Programming', 'license': 'GPL', 'author': ['Adam', 'Benny', 'Charlie']}, {'license': 'Apache 2.0', 'title': 'Business Modelling'}]}

assert dicttoxml(payload, fold_list=False, attr_type=False) == b'<?xml version="1.0" encoding="UTF-8" ?><root><book><title>Python Programming</title><license>GPL</license><author>Adam</author><author>Benny</author><author>Charlie</author></book><book><license>Apache 2.0</license><title>Business Modelling</title></book></root>'


def test_fix_dict_integer_key() -> None:
payload: dict = {1: "abc"}

assert dicttoxml(payload) == b'<?xml version="1.0" encoding="UTF-8" ?><root><n1 type="str">abc</n1></root>'

0 comments on commit 70689d7

Please sign in to comment.