Sir Neuman
Sir Neuman

Reputation: 1425

Cannot serialize data when patching to a field that has a 'valueschema' that is of type 'dict' in Eve

So say i have the following document:

test_obj = {
    'my_things':{
        'id17': {
            'blah': 3,
            'weird': 'yay',
            'thechallenge': ObjectId('5712d06fdb4d0856551300d2')
        },
        'id32': {
            'blah': 62,
            'weird': 'hoorah',
            'thechallenge': ObjectId('5712d06fdb4d0856551300d4')
        }
    },
    '_id': 12,
    'an_extra_field': 'asdf'
}

for this document i have the following schema:

API.config['DOMAIN']['test_obj']['schema'] = {
    'id': {'type': 'int'},
    'an_extra_field': {'type': 'string'},
    'my_things': {
        'type': 'dict',
        'valueschema': {
            'type': 'dict',
            'schema': {
                'blah': {'type': 'dict'},
                'weird': {'type': 'string'},
                'thechallenge': {'type': 'objectid'}
            }
        }
    }
}

Now say i make a patch with the following pseudocode:

data = {
    'mythings': {
        'id17': {
            'thechallenge': '5712d06fdb4d0856551300d8'
        }
    }
}
PATCH(url='/v1/test_objs/12', data=data)

When I make this patch Cerberus raises an error during validation, saying "value '5712d06fdb4d0856551300d8' cannot be converted to a ObjectId". Now this is a valid object id, and i find that if I make a patch to other non-valueschema fields it does not raise this error. It seems like valueschema was not meant to have a value of dict, and adding an extra 'schema' attribute was the only way i could get around cerberus raising a schemaerror/having cerberus actually validate my fields. But eve does not appear to actually be serializing my fields in my dictionary correctly. It should be of type ObjectId when it gets passed to Cerberus.

The way i'm temporarily getting around this is by manipulating my the code in Eve. In common.py (module) in serialize (function) in line 398 i added, where it checks if the field schema is a 'valueschema':

elif field_type == 'dict' and 'schema' in field_schema['valueschema']:
    for subdocument in document[field].values():
        serialize(subdocument, schema=field_schema['valueschema']['schema'])

Should i not be using type dict for the valueschema? If not how else should i handle this scenario? I would like to not have to maintain my own fork of Eve, so if others do want the ability to have valueschema be of type dict should i submit a pull-request for this change?

Upvotes: 1

Views: 188

Answers (1)

Nicola Iarocci
Nicola Iarocci

Reputation: 6576

This has been fixed with Eve v0.6.4, which has just been released.

Upvotes: 1

Related Questions