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

🐛 Fix converting dict having integer keys #5

Merged
merged 2 commits into from
Aug 13, 2022
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
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>'