Skip to content

PUT requests doesn't set default values for fields that have one defined #1280

@sunbit

Description

@sunbit

I've been upgrading an old Eve app to latest version, and we just find what we think is a bug introduced back at 2016 whith the 0.8 version. This is particulary bad for us as we're relyng on a field being set from a default value, as we're using that field for filtering the items of the resource.


Expected Behavior

After doing a PUT requests to replace an item, any field that has a default value should be set, as when doing an initial creation POST.

Minimal settings.py to reproduce the problem

MONGO_HOST = 'localhost'
MONGO_PORT = 27017
MONGO_DBNAME = 'apitest'
IF_MATCH = False
DOMAIN = {
    "test": {
        "schema": {
            "test_field": {"type": "string"},
            "default_field": {"type": "string", "default": "FOO", "allowed": ["FOO"]}
        },
        "resource_methods": ["POST", "GET"],
        "item_methods": ["GET", "PUT"]
    }
}

Minimal app to reproduce the problem

from eve import Eve
app = Eve()

if __name__ == '__main__':
    app.run()

Steps to reproduce it

curl -X POST -d '{"test_field": "value"}'  -H 'Content-Type: application/json' http://localhost:5000/test

curl -X GET http://localhost:5000/test/5cf55157f40227844c9b272c

{
  "_updated": "Mon, 03 Jun 2019 16:56:55 GMT",
  "default_field": "FOO",
  "test_field": "value",
 ...
  },
  "_created": "Mon, 03 Jun 2019 16:56:55 GMT",
  "_id": "5cf55157f40227844c9b272c"
}

curl -X PUT -d '{"test_field": "value2"}'  -H 'Content-Type: application/json' http://localhost:5000/test/5cf55157f40227844c9b272c

Actual Behavior

The default value is not set and so effetively deleted from the document after the PUT:

curl -X GET http://localhost:5000/test/5cf55157f40227844c9b272c

{
  "_updated": "Mon, 03 Jun 2019 16:58:00 GMT",
  "_created": "Mon, 03 Jun 2019 16:56:55 GMT",
  "test_field": "value2",
  "_id": "5cf55157f40227844c9b272c",
  ...
  }
}

I actually identified where it was introduced. The point were the old behaviour was removed from eve.io.mongo.validation and the new one added in eve.validation:

2d49d2c#diff-c87b89d17eb3575930289d6cee3eae22L82

2d49d2c#diff-53b54c5da23c48df3198ad3b0fbb7a0fR35

The point here is if this is considered the wanted behaviour or something that has been broken. It would be useful to know @nicolaiarocci opinion about this, to be able to fix it properly. Looking at the code i see probably there are other features that are the cause of that changes that must be preserved.

Environment

  • Python version: 2.7.10
  • Eve version: 0.9.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No 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