Skip to content

Error when a key is a number #35

@jacobg

Description

@jacobg

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions