Skip to content

Commit

Permalink
handle suffix with not traversable objects (#1649)
Browse files Browse the repository at this point in the history
* handle suffix with @@ that are not traversable

* blacked

* fix suffix segements order

* comments

* add changelog

---------

Co-authored-by: Mauro Amico <mauro.amico@gmail.com>
Co-authored-by: David Glick <david@glicksoftware.com>
  • Loading branch information
3 people committed Jun 26, 2023
1 parent 2048301 commit eb102cc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions news/1649.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix path2uid method, to handle suffix with non-traversable objects. @cekk @mamico
10 changes: 8 additions & 2 deletions src/plone/restapi/deserializer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@ def path2uid(context, link):
path = "{portal_path}/{path}".format(
portal_path=portal_path, path=path.lstrip("/")
)

# handle edge-case when we have non traversable path like /@@download/file
if "/@@" in path:
path, suffix = path.split("/@@", 1)
suffix = "/@@" + suffix
else:
suffix = ""
obj = portal.unrestrictedTraverse(path, None)
if obj is None or obj == portal:
return link
segments = path.split("/")
suffix = ""
while not IUUIDAware.providedBy(obj):
obj = aq_parent(obj)
if obj is None:
break
suffix += "/" + segments.pop()
suffix = "/" + segments.pop() + suffix
# check if obj is wrong because of acquisition
if not obj or "/".join(obj.getPhysicalPath()) != "/".join(segments):
return link
Expand Down
29 changes: 28 additions & 1 deletion src/plone/restapi/tests/test_resolveuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from plone.restapi.interfaces import IFieldSerializer
from plone.restapi.testing import PLONE_RESTAPI_BLOCKS_FUNCTIONAL_TESTING
from plone.restapi.testing import PLONE_RESTAPI_BLOCKS_INTEGRATION_TESTING
from plone.restapi.tests.performance import set_file
from plone.uuid.interfaces import IUUID
from Products.CMFCore.utils import getToolByName
from unittest import TestCase
Expand Down Expand Up @@ -537,11 +538,37 @@ def test_path_keeps_suffix(self):
]["url"],
f"../resolveuid/{uid}/view",
)

def test_path_keeps_suffix_also_with_non_traversable_items(self):
self.portal.invokeFactory("File", id="file", title="File")
file_obj = self.portal.file
set_file(file_obj)
uid = IUUID(file_obj)

blocks = {
"effbdcdc-253c-41a7-841e-5edb3b56ce32": {
"@type": "text",
"text": {
"entityMap": {
"0": {
"data": {
"href": file_obj.absolute_url() + "/@@download/file",
"rel": "nofollow",
"url": file_obj.absolute_url() + "/@@download/file",
},
"mutability": "MUTABLE",
"type": "LINK",
}
}
},
}
}
value = self.deserialize("blocks", blocks)
self.assertEqual(
value["effbdcdc-253c-41a7-841e-5edb3b56ce32"]["text"]["entityMap"]["0"][
"data"
]["url"],
f"../resolveuid/{uid}/view",
f"../resolveuid/{uid}/@@download/file",
)

def test_blocks_field_serialization_resolves_uids_with_primary_field_url(self):
Expand Down

0 comments on commit eb102cc

Please sign in to comment.