Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/fix_image_scale_bloat.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimization: Avoid redundant object updates in PATCH requests when image content is identical to existing content.
59 changes: 59 additions & 0 deletions src/plone/restapi/tests/test_content_patch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from importlib import import_module
from OFS.interfaces import IObjectWillBeAddedEvent
from plone.app.testing import login
from plone.app.testing import setRoles
Expand All @@ -19,6 +20,10 @@
import transaction
import unittest

HAS_PLONE_62 = getattr(
import_module("Products.CMFPlone.factory"), "PLONE62MARKER", False
)


class TestContentPatch(unittest.TestCase):

Expand Down Expand Up @@ -197,3 +202,57 @@ def test_patch_document_with_apostrophe_dont_return_500(self):
self.assertEqual(204, response.status_code)
transaction.begin()
self.assertEqual("<p>example with '</p>", self.portal.doc1.text.raw)

@unittest.skipUnless(HAS_PLONE_62, "Requires Plone 6.2+")
def test_patch_image_redundant_no_event(self):
image_data = "R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs="
response = requests.post(
self.portal.absolute_url(),
headers={"Accept": "application/json"},
auth=(SITE_OWNER_NAME, SITE_OWNER_PASSWORD),
json={
"@type": "Image",
"image": {
"data": image_data,
"encoding": "base64",
"content-type": "image/gif",
"filename": "test.gif",
},
},
)
self.assertEqual(201, response.status_code)
image_url = response.json()["@id"]
transaction.commit()

# Track ObjectModifiedEvent
sm = getGlobalSiteManager()
fired_events = []

def record_event(event):
fired_events.append(event)

sm.registerHandler(record_event, (IObjectModifiedEvent,))

# Patch with same data
response = requests.patch(
image_url,
headers={"Accept": "application/json"},
auth=(SITE_OWNER_NAME, SITE_OWNER_PASSWORD),
json={
"image": {
"data": image_data,
"encoding": "base64",
"content-type": "image/gif",
"filename": "test.gif",
},
},
)
self.assertEqual(204, response.status_code)

# In current Plone, this will be 1 because of identity mismatch
# We want it to be 0
self.assertEqual(
len(fired_events), 0, "ObjectModifiedEvent was fired for redundant PATCH"
)

sm.unregisterHandler(record_event, (IObjectModifiedEvent,))
Loading