Skip to content

Commit

Permalink
Fix status code (back to 400) if a referenced object can not
Browse files Browse the repository at this point in the history
be resolved during deserialization.
  • Loading branch information
lukasgraf committed Jun 25, 2019
1 parent d6597a5 commit 11e9989
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions news/777.bugfix
@@ -0,0 +1,2 @@
Fix status code (back to 400) if a referenced object can not be resolved during deserialization.
[lgraf]
1 change: 1 addition & 0 deletions src/plone/restapi/deserializer/relationfield.py
Expand Up @@ -54,6 +54,7 @@ def __call__(self, value):
resolved_by = "UID"

if obj is None:
self.request.response.setStatus(400)
raise ValueError(
u"Could not resolve object for {}={}".format(resolved_by, value)
)
Expand Down
4 changes: 4 additions & 0 deletions src/plone/restapi/tests/test_dxfield_deserializer.py
Expand Up @@ -421,6 +421,7 @@ def test_relationchoice_deserialization_from_invalid_intid_raises(self):
self.assertEqual(
str(cm.exception), u"Could not resolve object for intid=123456789"
)
self.assertEqual(400, self.request.response.getStatus())

def test_relationchoice_deserialization_from_invalid_uid_raises(self):
with self.assertRaises(ValueError) as cm:
Expand All @@ -432,6 +433,7 @@ def test_relationchoice_deserialization_from_invalid_uid_raises(self):
str(cm.exception),
u"Could not resolve object for UID=ac12b24913cf45c6863937367aacc263",
)
self.assertEqual(400, self.request.response.getStatus())

def test_relationchoice_deserialization_from_invalid_url_raises(self):
with self.assertRaises(ValueError) as cm:
Expand All @@ -443,6 +445,7 @@ def test_relationchoice_deserialization_from_invalid_url_raises(self):
str(cm.exception),
u"Could not resolve object for URL=http://nohost/plone/doesnotexist",
)
self.assertEqual(400, self.request.response.getStatus())

def test_relationchoice_deserialization_from_invalid_path_raises(self):
with self.assertRaises(ValueError) as cm:
Expand All @@ -452,6 +455,7 @@ def test_relationchoice_deserialization_from_invalid_path_raises(self):
self.assertEqual(
str(cm.exception), u"Could not resolve object for path=/doesnotexist"
)
self.assertEqual(400, self.request.response.getStatus())

def test_relationlist_deserialization_returns_list_of_documents(self):
doc2 = self.portal[
Expand Down

0 comments on commit 11e9989

Please sign in to comment.