Skip to content

Commit

Permalink
Merge pull request #5 from Ousret/bugfix-dict-integer-key
Browse files Browse the repository at this point in the history
🐛 Fix converting dict having integer keys
  • Loading branch information
Ousret authored Aug 13, 2022
2 parents 5e4d309 + 5dbd28d commit 784e1ff
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Version 2.1.0
* Changes:
* Handling bool values properly
* Support for disabling default list folding
* Fix converting dict having integer as key
* Add /docs

Version 2.0.0
Expand Down
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 784e1ff

Please sign in to comment.