Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating a document with a data_relation that is nullable fails #1159

Closed
jdalegonzalez opened this issue May 30, 2018 · 7 comments
Closed

Updating a document with a data_relation that is nullable fails #1159

jdalegonzalez opened this issue May 30, 2018 · 7 comments
Labels
Milestone

Comments

@jdalegonzalez
Copy link

Expected Behavior

Given a schema that defines a data_relation but also marks the field as "nullable", one would expect that setting the field to "None" would be legal.

### Given the following schema....
USER_SCHEMA = {
    'email': {
        'type': 'string',
        'minlength': 1,
        'maxlength': 254,
        'required': True,
        'unique': True,
    },
    'name': {
        'type': 'string',
    },
    'title': {
        'type': 'string',
    },
    employer: {
        'type': 'objectid',
        'required': False,
        'nullable': True,
        'data_relation': {
            'resource': 'company,
            'field': '_id',
            'embeddable': True
        },
    }
}

#### The following patch will fail but should succeed.
def test_patch_user():
    payload = {'employer': None}
    patch_header = {
        'Authorization': TOKEN1['Authorization'],
        'If-Match': user_etag
    }
    req = requests.patch(BASE_URL + '/users/' + user_id,
                         headers=patch_header, json=payload)
    assert req.status_code == 200

Actual Behavior

Instead, eve generates an error saying: "value 'None' must exist in resource 'companies', field '_id'."

Environment

  • Python version: 3.6.5
  • Eve version: 0.8
@nicolaiarocci nicolaiarocci added this to the 0.8.1 milestone May 31, 2018
@nicolaiarocci
Copy link
Member

Excellent, thank you.

@jdalegonzalez
Copy link
Author

For those folks that have this issue and don't want to wait for a fix, I temporarily solved it by extending the validator and overriding the _validate_data_relation like so....

class MyValidator(Validator):
    def _validate_data_relation(self, relation, field, value):
        """ {'type': 'dict',
             'schema': {
                'resource': {'type': 'string', 'required': True},
                'field': {'type': 'string', 'required': True},
                'embeddable': {'type': 'boolean', 'default': False},
                'version': {'type': 'boolean', 'default': False}
             }} """

        if value:
            super(C2M2validator, self)._validate_data_relation(
                relation, field, value
            )
    # def _validate_data_relation

@nicolaiarocci
Copy link
Member

Should be fixed now!

@Kobzol
Copy link

Kobzol commented Aug 1, 2018

I'm having the same error and it still doesn't work even with Eve eb6d37d. I'm sending the ref in JSON with value null, so probably the conversion from null to None doesn't work properly.

@nicolaiarocci
Copy link
Member

@Kobzol can't reproduce it.

Sending a {"employer": null} document to Eve 0.8.1-dev via Postman, to an endpoint defined as in the test added with eb6d37d returns 201 with the following document:

{
    "employer": null,
    "_updated": "Wed, 08 Aug 2018 08:11:16 GMT",
    "_created": "Wed, 08 Aug 2018 08:11:16 GMT",
    "_etag": "3d9eb0dcb165523586d796aff503401aacf5709d",
    "_id": "5b6aa5a4ee9f91392db8ad4c",
    "_links": {
        "self": {
            "title": "Employee",
            "href": "employee/5b6aa5a4ee9f91392db8ad4c"
        }
    },
    "_status": "OK"
}

@nicolaiarocci
Copy link
Member

PS: if I switch back to v0.8 (current stable), I get a 422 as reported and expected

@Kobzol
Copy link

Kobzol commented Aug 8, 2018

Sorry, I guess that I didn't update the version correctly using pip, I tried it with a clean clone and now it works. Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants