Skip to content

Commit

Permalink
Fix #1600 UnboundLocalError in RelationChoice deserializer
Browse files Browse the repository at this point in the history
* Fix UnboundLocalError in RelationChoice deserializer
* changelog
* update tests
* black
  • Loading branch information
davisagli committed Mar 20, 2023
1 parent 193884e commit 7696dc7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions news/1600.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix UnboundLocalError in RelationChoice deserializer. @davisagli
6 changes: 5 additions & 1 deletion src/plone/restapi/deserializer/relationfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class RelationChoiceFieldDeserializer(DefaultFieldDeserializer):
def __call__(self, value):
obj = None
resolved_by = None

if isinstance(value, dict):
# We are trying to deserialize the output of a serialization
Expand Down Expand Up @@ -50,7 +51,10 @@ def __call__(self, value):

if obj is None:
self.request.response.setStatus(400)
raise ValueError(f"Could not resolve object for {resolved_by}={value}")
msg = f"Could not resolve object for {value}"
if resolved_by:
msg += f" (resolved by {resolved_by})"
raise ValueError(msg)

self.field.validate(obj)
return obj
16 changes: 12 additions & 4 deletions src/plone/restapi/tests/test_dxfield_deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ def test_relationchoice_deserialization_from_invalid_intid_raises(self):
with self.assertRaises(ValueError) as cm:
self.deserialize("test_relationchoice_field", 123456789)
self.assertEqual(
str(cm.exception), "Could not resolve object for intid=123456789"
str(cm.exception),
"Could not resolve object for 123456789 (resolved by intid)",
)
self.assertEqual(400, self.request.response.getStatus())

Expand All @@ -433,7 +434,7 @@ def test_relationchoice_deserialization_from_invalid_uid_raises(self):
)
self.assertEqual(
str(cm.exception),
"Could not resolve object for UID=ac12b24913cf45c6863937367aacc263",
"Could not resolve object for ac12b24913cf45c6863937367aacc263 (resolved by UID)",
)
self.assertEqual(400, self.request.response.getStatus())

Expand All @@ -445,18 +446,25 @@ def test_relationchoice_deserialization_from_invalid_url_raises(self):
)
self.assertEqual(
str(cm.exception),
"Could not resolve object for URL=http://nohost/plone/doesnotexist",
"Could not resolve object for http://nohost/plone/doesnotexist (resolved by URL)",
)
self.assertEqual(400, self.request.response.getStatus())

def test_relationchoice_deserialization_from_invalid_path_raises(self):
with self.assertRaises(ValueError) as cm:
self.deserialize("test_relationchoice_field", "/doesnotexist")
self.assertEqual(
str(cm.exception), "Could not resolve object for path=/doesnotexist"
str(cm.exception),
"Could not resolve object for /doesnotexist (resolved by path)",
)
self.assertEqual(400, self.request.response.getStatus())

def test_relationchoice_deserialization_from_wrong_type_raises(self):
with self.assertRaises(ValueError) as cm:
self.deserialize("test_relationchoice_field", None)
self.assertEqual(str(cm.exception), "Could not resolve object for None")
self.assertEqual(400, self.request.response.getStatus())

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

0 comments on commit 7696dc7

Please sign in to comment.