diff --git a/news/1583.bugfix b/news/1583.bugfix new file mode 100644 index 000000000..9b021032b --- /dev/null +++ b/news/1583.bugfix @@ -0,0 +1,2 @@ +Provide a JsonCompatible adapter for RichTextValue objects +[erral] \ No newline at end of file diff --git a/src/plone/restapi/serializer/configure.zcml b/src/plone/restapi/serializer/configure.zcml index 32c63b2d7..2f740822e 100644 --- a/src/plone/restapi/serializer/configure.zcml +++ b/src/plone/restapi/serializer/configure.zcml @@ -85,6 +85,7 @@ + diff --git a/src/plone/restapi/serializer/converters.py b/src/plone/restapi/serializer/converters.py index 6da75b754..7e6f424c8 100644 --- a/src/plone/restapi/serializer/converters.py +++ b/src/plone/restapi/serializer/converters.py @@ -78,8 +78,7 @@ def default_converter(value): return value raise TypeError( - "No converter for making" - " {!r} ({}) JSON compatible.".format(value, type(value)) + "No converter for making {!r} ({}) JSON compatible.".format(value, type(value)) ) @@ -173,6 +172,12 @@ def timedelta_converter(value): return json_compatible(value.total_seconds()) +@adapter(IRichTextValue) +@implementer(IJsonCompatible) +def richtextvalue_converter(value): + return json_compatible(value.output) + + @adapter(IRichTextValue, IDexterityContent) @implementer(IContextawareJsonCompatible) class RichtextDXContextConverter: diff --git a/src/plone/restapi/tests/test_converters.py b/src/plone/restapi/tests/test_converters.py new file mode 100644 index 000000000..262dc232d --- /dev/null +++ b/src/plone/restapi/tests/test_converters.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- + +import unittest +from plone.app.textfield import RichTextValue +from plone.restapi.interfaces import IJsonCompatible +from plone.restapi.testing import PLONE_RESTAPI_DX_INTEGRATION_TESTING + + +class TestConverters(unittest.TestCase): + + layer = PLONE_RESTAPI_DX_INTEGRATION_TESTING + + def test_richtextvalue_converter(self): + """test that a RichTextValue is converted to a proper HTML""" + html = "

This is a demo HTML

" + value = RichTextValue(html, "text/html", "text/html") + json_compatible_value = IJsonCompatible(value) + self.assertEqual(json_compatible_value, html) diff --git a/src/plone/restapi/tests/test_types.py b/src/plone/restapi/tests/test_types.py index a19186ca0..5dbb222fb 100644 --- a/src/plone/restapi/tests/test_types.py +++ b/src/plone/restapi/tests/test_types.py @@ -46,7 +46,9 @@ class ITaggedValuesSchema(model.Schema): parametrized_widget_field = schema.TextLine(title="Parametrized widget field") form.widget( - "parametrized_widget_field", a_param="some_value", defaultFactory=lambda: "Foo" + "parametrized_widget_field", + a_param="some_value", + defaultFactory=lambda: "Foo", ) not_parametrized_widget_field = schema.TextLine( @@ -459,7 +461,11 @@ def test_decimal(self): def test_int(self): field = schema.Int( - title="My field", description="My great field", min=0, max=100, default=50 + title="My field", + description="My great field", + min=0, + max=100, + default=50, ) adapter = getMultiAdapter( (field, self.portal, self.request), IJsonSchemaProvider @@ -742,9 +748,33 @@ def test_richtext(self): adapter.get_schema(), ) + def test_richtext_with_default(self): + field = RichText( + title="My field", + description="My great field", + default="

Some default value

", + ) + adapter = getMultiAdapter( + (field, self.portal, self.request), IJsonSchemaProvider + ) + + self.assertEqual( + { + "type": "string", + "title": "My field", + "factory": "Rich Text", + "description": "My great field", + "widget": "richtext", + "default": "

Some default value

", + }, + adapter.get_schema(), + ) + def test_date(self): field = schema.Date( - title="My field", description="My great field", default=date(2016, 1, 1) + title="My field", + description="My great field", + default=date(2016, 1, 1), ) adapter = getMultiAdapter( (field, self.portal, self.request), IJsonSchemaProvider @@ -781,7 +811,9 @@ def test_datetime(self): def test_jsonfield(self): field = JSONField( - title="My field", description="My great field", widget="my_widget_name" + title="My field", + description="My great field", + widget="my_widget_name", ) adapter = getMultiAdapter( (field, self.portal, self.request), IJsonSchemaProvider