jpadilla / django-rest-framework-xml

XML support for Django REST Framework

Home Page:http://jpadilla.github.io/django-rest-framework-xml

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error when a key is a number

jacobg opened this issue · comments

The job of django-rest-framework-xml is to convert dictionaries to xml. It is valid for a python dictionary to contain a key which is either an integer or the string representation of an integer. And it renders find to json. But the xml renderer fails: (i) if the key is an integer, sax will raise an exception; (2) if it's a string rep of an integer, it will render to invalid xml because an xml tag name cannot be a number.

I propose we slightly modify the renderer's _to_xml method to add this code when iterating each key:

                attributes = {}
                if isinstance(key, six.integer_types):
                    attributes = {'number': force_text(key)}
                    key = 'element'

Does that seem reasonable?