pyeve / eve

REST API framework designed for human beings
https://python-eve.org
Other
6.71k stars 745 forks source link

Type serialization does not work inside valuesrules or valueschema #1511

Open absassi opened 1 year ago

absassi commented 1 year ago

Cerberus 1.3 deprecates keyschema and valueschema in favor of keysrules and valuesrules. It also modifies any schema passed to it to rename the deprecated names with the corresponding new names.

However, Eve still uses valueschema in eve.methods.common.serialize (eve/methods/common.py line 523) and doesn't look for valuesrules. This means that if the domain configuration contains a valuesrules (or valueschema - it doesn't matter, because Cerberus renames them) with a type that requires conversion (such as datetime, for instance), that conversion will not happen.

Unfortunately, downgrading to Cerberus 1.2 is not an option for me, as it doesn't work in Python 3.11.


Expected Behavior

The following minimal example program and request should successfully insert a new document in the "example" collection:

from eve import Eve

app = Eve(
    settings={
        "DOMAIN": {
            "example": {
                "resource_methods": ["POST"],
                "schema": {
                    "field1": {
                        "type": "dict",
                        "valueschema": {"type": "datetime"},
                    },
                    "field2": {
                        "type": "dict",
                        "valuesrules": {"type": "datetime"},
                    },
                },
            },
        },
    })
app.run()
curl -XPOST -H 'Content-type: application/json' --data '{"field1": {"a": "Fri, 04 Aug 2023 12:26:15 GMT"}, "field2": {"a": "Fri, 04 Aug 2023 12:26:15 GMT"}}' http://localhost:5000/example

Actual Behavior

Eve does not convert the strings to datetime before passing them on to validation, as it would if the "type": "datetime" were not inside valuesrules. The request then fails with:

{"_status": "ERR", "_issues": {"field1": {"a": "must be of datetime type"}, "field2": {"a": "must be of datetime type"}}, "_error": {"code": 422, "message": "Insertion failure: 1 document(s) contain(s) error(s)"}}

Environment