coleifer / flask-peewee

flask integration for peewee, including admin, authentication, rest api and more
http://flask-peewee.readthedocs.org/
MIT License
777 stars 179 forks source link

Serializer clean_data doesn't work with array values #146

Open jhorman opened 10 years ago

jhorman commented 10 years ago

https://github.com/coleifer/flask-peewee/blob/0.6.5/flask_peewee/serializer.py#L25

elif isinstance(value, (list, tuple)):
  data[key] = map(self.clean_data, value)

This fails in a few ways. clean_data will be called, but actually will receive the first item in the array, since map accepts *args. Even if the array was passed through, it will not work b/c array doesn't have a .items() method. I think a safer implementation would be something like

    def clean_data(self, data):
        if isinstance(data, dict):
            for key, value in data.items():
                data[key] = self.clean_data(value)
        elif isinstance(data, (list, tuple)):
            for i, item in enumerate(data):
                data[i] = self.clean_data(item)
        else:
            data = self.convert_value(data)

        return data
coleifer commented 10 years ago

I think the assumption is that the list contains dicts. I'm not sure about map taking *args.

jhorman commented 10 years ago

My use was I was trying to serialize an arbitrary JSONField out, and it failed. I could have dicts of lists of lists of dicts, etc.