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

Comments

Projects
None yet
3 participants
@jdalegonzalez

jdalegonzalez commented May 30, 2018

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 the bug label May 31, 2018

@nicolaiarocci nicolaiarocci added this to the 0.8.1 milestone May 31, 2018

@nicolaiarocci

This comment has been minimized.

Member

nicolaiarocci commented May 31, 2018

Excellent, thank you.

@jdalegonzalez

This comment has been minimized.

jdalegonzalez commented May 31, 2018

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

This comment has been minimized.

Member

nicolaiarocci commented Jun 1, 2018

Should be fixed now!

@Kobzol

This comment has been minimized.

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

This comment has been minimized.

Member

nicolaiarocci commented Aug 8, 2018

@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

This comment has been minimized.

Member

nicolaiarocci commented Aug 8, 2018

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

@Kobzol

This comment has been minimized.

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