Skip to content

Commit

Permalink
Fix: write failure on nullable data relation fields
Browse files Browse the repository at this point in the history
Closes #1159.
  • Loading branch information
nicolaiarocci committed Jun 1, 2018
1 parent de2e457 commit 17aa702
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Unreleased

Fixed
~~~~~
- Updating a field with a nullable data relation fails when value is null (`#1159`_)
- ``cerberus.schema.SchemaError`` when ``VALIDATE_FILTERS = True``. (`#1154`_)
- Serializers fails when array of types is in schema. (`#1112`_)
- Replace the broken ``make audit`` shortcut with ``make check``, add the
Expand Down Expand Up @@ -52,6 +53,7 @@ Docs
.. _`#1156`: https://github.com/pyeve/eve/issues/1156
.. _`#1157`: https://github.com/pyeve/eve/issues/1157
.. _`#1158`: https://github.com/pyeve/eve/issues/1158
.. _`#1159`: https://github.com/pyeve/eve/issues/1159

Version 0.8
-----------
Expand Down
2 changes: 1 addition & 1 deletion eve/io/mongo/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def _validate_data_relation(self, data_relation, field, value):
'embeddable': {'type': 'boolean', 'default': False},
'version': {'type': 'boolean', 'default': False}
}} """
if not value and config.DOMAIN[self.resource]["schema"][field].get("nullable"):
if not value and self.schema[field].get("nullable"):
return

if "version" in data_relation and data_relation["version"] is True:
Expand Down
16 changes: 10 additions & 6 deletions eve/tests/methods/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,21 +892,25 @@ def test_post_updating_a_document_with_nullable_data_relation_does_not_fail(self
# See #1159.
del (self.domain["contacts"]["schema"]["ref"]["required"])

user_schema = {
employee = {
"employer": {
"type": "objectid",
"nullable": True,
"data_relation": {"resource": self.known_resource, "field": "_id"},
"data_relation": {"resource": self.known_resource},
}
}
self.app.register_resource("user_schema", {"schema": user_schema})
self.app.register_resource("employee", {"schema": employee})

data = {"employer": None}
r, s = self.post("user_schema", data=data)
r, s = self.post("employee", data=data)
self.assert201(s)

user_schema["employer"]["nullable"] = False
r, s = self.post("user_schema", data=data)
employee["employer"]["nullable"] = False
r, s = self.post("employee", data=data)
self.assert422(s)

del (employee["employer"]["nullable"])
r, s = self.post("employee", data=data)
self.assert422(s)

def perform_post(self, data, valid_items=[0]):
Expand Down

0 comments on commit 17aa702

Please sign in to comment.