pyeve / eve

REST API framework designed for human beings

Home Page:https://python-eve.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] Default values are being applied for PATCH if it is executed against nested field

0x113 opened this issue · comments

Whenever a PATCH request is executed against an item that has nested fields specified with the default values, the default values are being applied.

Schema

schema = {
    'name': {
        'type': 'string',
        'minlength': 1,
        'maxlength': 15,
        'required': True,
    },
    'type': {
        'type': 'string',
        'default': 'STANDARD',
        'allowed': ['STANDARD', 'PREMIUM']
    },
    'status': {
        'type': 'dict',
        'schema': {
            'state': {'type': 'string', 'required': False, 'default': 'CREATED', 'allowed': ['CREATED', 'ACTIVE', 'DELETED'], 'nullable': False},
            'is_admin': {'type': 'boolean', 'required': False, 'default': False, 'nullable': False},
            'created_by': {'type': 'string', 'required': True, 'nullable': False}
        },
        'nullable': False
    },
}

Initial item

GET http://127.0.0.1:5000/accounts/610d01fa69b637a9a6f16b1f
{
    "_id": "610d01fa69b637a9a6f16b1f",
    "name": "TestAccount",
    "status": {
        "state": "CREATED",
        "is_admin": true,
        "created_by": "admin"
    },
    "type": "STANDARD",
    ...
    "_status": "OK"
}

Now, I want to update the state field in the status:

PATCH http://127.0.0.1:5000/accounts/610d01fa69b637a9a6f16b1f
{
    "status": {
        "state": "ACTIVE"
    }
}

It results in such a response:

GET http://127.0.0.1:5000/accounts/610d01fa69b637a9a6f16b1f

{
    "_id": "610d01fa69b637a9a6f16b1f",
    "name": "TestAccount",
    "status": {
        "state": "ACTIVE", // <-- OK
        "is_admin": false, // <-- Default value applied
        "created_by": "admin"
    },
    "type": "STANDARD",
    ...
    "_status": "OK"
}

Expected Behavior

The default values should not be applied for the PATCH method.

Actual Behavior

The default values are being applied for the PATCH method if it is executed against a nested field. As shown in the example, the state field changed to ACTIVE, but also the is_admin field was reset to its default value.

Environment

  • Python version: Python 3.8.10

  • Eve version: v1.1.5

@nicolaiarocci

Actually, it's expected behavior, and can be disabled after setting NORMALIZE_ON_PATCH to False. Docs: https://docs.python-eve.org/en/stable/features.html#editing-a-document-patch