Skip to content

Serializer fails when array of types is in schema #1112

@Kobzol

Description

@Kobzol

When an array of types is used in a schema ('type': ['string', 'number']), the Eve serializer (the function serialize in common.py) will fail with the exception 'unhashable list'. This can be hard to find, because the serializer swallows exceptions and sometimes the request will go through OK, but sometimes it will fail because the serializer didn't serialize everything correctly.

Example (the exception is thrown with this schema at common.py:423):

{
    'val': {
        'type': 'dict',
        'schema': {
            'x': {
                'type': ['string', 'number'],
                'required': True
            },
            'timestamp': {
                'type': 'datetime',
                'required': False
            }
        }
    }
}

When I POST a new item to this schema, it will either insert it and return 201 or return 422 with the error "timestamp": "must be of datetime type". This depends on the order in which the serializer goes through the fields, which is non-deterministic. Either it serializes the datetime first, then it's ok, or it finds the constraint 'type': ['string', 'number'] which stops the serializer, the datetime stays unparsed and the request fails.

Is array of types supported by Eve? It is officially supported by Cerberus. I tried replacing it with 'anyof_type': ['string', 'number'], but that returns an error (no definitions validate).

Version: 0.8-dev from latest github master (commit sha 1d4a308)
Python version: happens both on 2.7 and 3.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions