diff --git a/src/plone/restapi/serializer/blocks.py b/src/plone/restapi/serializer/blocks.py index 3d2f564a8f..da1c4a2e62 100644 --- a/src/plone/restapi/serializer/blocks.py +++ b/src/plone/restapi/serializer/blocks.py @@ -220,25 +220,22 @@ def _process_data(self, data, field=None): data["overwrite"] = True return data - if data.get("overwrite"): - # Editor decided to overwrite a data - return data - if isinstance(value, str): url = value + value = [{"@id": url}] else: url = value[0].get("@id", "") brain = url_to_brain(url) - if brain is not None: serialized_brain = getMultiAdapter( (brain, self.request), ISerializeToJsonSummary )() - # Fields from the teaser-schema need to be overwritten - for key in ["title", "description", "head_title"]: - if key in serialized_brain: - data[key] = serialized_brain[key] + if not data.get("overwrite"): + # Update fields at the top level of the block data + for key in ["title", "description", "head_title"]: + if key in serialized_brain: + data[key] = serialized_brain[key] # We return the serialized brain. value[0].update(serialized_brain) diff --git a/src/plone/restapi/tests/test_blocks_serializer.py b/src/plone/restapi/tests/test_blocks_serializer.py index 9005e2a406..a1c378ad84 100644 --- a/src/plone/restapi/tests/test_blocks_serializer.py +++ b/src/plone/restapi/tests/test_blocks_serializer.py @@ -517,3 +517,97 @@ def test_image_scales_serializer_is_json_compatible(self): blocks={"123": {"@type": "image", "url": f"../resolveuid/{image_uid}"}}, ) self.assertIs(type(res["123"]["image_scales"]), dict) + + def test_teaser_block_serializer_dynamic(self): + doc = self.portal["doc1"] + doc_uid = doc.UID() + resolve_uid_link = f"../resolveuid/{doc_uid}" + value = self.serialize( + context=self.portal.doc1, + blocks={ + "1": { + "@type": "teaser", + "href": resolve_uid_link, + "overwrite": False, + } + }, + ) + + block = value["1"] + self.assertEqual(block["title"], doc.title) + self.assertEqual(block["description"], doc.description) + href = block["href"][0] + self.assertEqual(href["@id"], doc.absolute_url()) + + def test_teaser_block_serializer_dynamic_nested(self): + doc = self.portal["doc1"] + doc_uid = doc.UID() + resolve_uid_link = f"../resolveuid/{doc_uid}" + value = self.serialize( + context=self.portal.doc1, + blocks={ + "grid": { + "@type": "gridBlock", + "blocks": { + "1": { + "@type": "teaser", + "href": resolve_uid_link, + "overwrite": False, + }, + }, + "blocks_layout": {"items": ["1"]}, + } + }, + ) + + block = value["grid"]["blocks"]["1"] + self.assertEqual(block["title"], doc.title) + self.assertEqual(block["description"], doc.description) + href = block["href"][0] + self.assertEqual(href["@id"], doc.absolute_url()) + + def test_teaser_block_serializer_with_overwrite(self): + doc = self.portal["doc1"] + doc_uid = doc.UID() + resolve_uid_link = f"../resolveuid/{doc_uid}" + value = self.serialize( + context=self.portal.doc1, + blocks={ + "1": { + "@type": "teaser", + "href": resolve_uid_link, + "overwrite": True, + "title": "Custom title", + "description": "Custom description", + } + }, + ) + + block = value["1"] + self.assertEqual(block["title"], "Custom title") + self.assertEqual(block["description"], "Custom description") + href = block["href"][0] + self.assertEqual(href["@id"], doc.absolute_url()) + + def test_teaser_block_serializer_legacy(self): + # no "overwrite" key -> default to True + doc = self.portal["doc1"] + doc_uid = doc.UID() + resolve_uid_link = f"../resolveuid/{doc_uid}" + value = self.serialize( + context=self.portal.doc1, + blocks={ + "1": { + "@type": "teaser", + "href": [{"@id": resolve_uid_link}], + "title": "Custom title", + "description": "Custom description", + } + }, + ) + + block = value["1"] + self.assertEqual(block["title"], "Custom title") + self.assertEqual(block["description"], "Custom description") + href = block["href"][0] + self.assertEqual(href["@id"], doc.absolute_url())