plone.restapi icon indicating copy to clipboard operation
plone.restapi copied to clipboard

Provide IJsonCompatible adapter for RichTextValue objects

Open erral opened this issue 2 years ago • 1 comments

When defining a default value in a RichText field, this value needs to be passed through a IJsonCompatible adapter, but we are missing the converter for RichTextValue objects, and thus the edit form rendering breaks in Volto.

Dexterity object definition:

    download_full_dataset_text = RichText(
        title=_(
            'Text of the "Download full dataset entry',
        ),
        description=_(
            "Enter the text that will be shown in the Download tab",
        ),
        default=(
            "<p>If you want to download the full dataset, click <a"
            ' href="/en/how-to-guides/how-to-download-spatial-data/how-to-download-m2m">here</a>'
            " to learn more.</p>"
        ),
        required=False,
        readonly=False,
    )

plone.restapi fail when rendering the Volto edit form:

2023-02-13 17:05:44,536 ERROR   [Zope.SiteErrorLog:22][waitress-1] TypeError: http://localhost:3000/en/@types/DataSet
Traceback (innermost last):
  Module ZPublisher.WSGIPublisher, line 162, in transaction_pubevents
  Module ZPublisher.WSGIPublisher, line 371, in publish_module
  Module ZPublisher.WSGIPublisher, line 266, in publish
  Module ZPublisher.mapply, line 85, in mapply
  Module ZPublisher.WSGIPublisher, line 63, in call_object
  Module plone.rest.service, line 22, in __call__
  Module plone.restapi.services, line 19, in render
  Module plone.restapi.services.types.get, line 114, in reply
  Module plone.restapi.services.types.get, line 136, in reply_for_type
  Module plone.restapi.types.utils, line 329, in get_info_for_type
  Module plone.restapi.types.utils, line 272, in get_jsonschema_for_portal_type
  Module plone.restapi.types.utils, line 261, in get_jsonschema_for_fti
  Module zope.component.hooks, line 135, in adapter_hook
  Module plone.restapi.serializer.converters, line 136, in dict_converter
  Module plone.restapi.serializer.converters, line 68, in json_compatible
  Module zope.component.hooks, line 135, in adapter_hook
  Module plone.restapi.serializer.converters, line 136, in dict_converter
  Module plone.restapi.serializer.converters, line 68, in json_compatible
  Module zope.component.hooks, line 135, in adapter_hook
  Module plone.restapi.serializer.converters, line 80, in default_converter
TypeError: No converter for making RichTextValue object. (Did you mean <attribute>.raw or <attribute>.output?) (<class 'plone.app.textfield.value.RichTextValue'>) JSON compatible.

erral avatar Feb 13 '23 16:02 erral

The problem is a bit more subtle.

There is already an IJsonCompatible adapter for RichTextValue: https://github.com/plone/plone.restapi/blob/master/src/plone/restapi/serializer/converters.py#L178

It is even documented: https://6.docs.plone.org/plone.restapi/docs/source/usage/serialization.html#richtext-fields

The problem is that it is a converter which requires a context (in order to resolve any relative links within HTML rich text, IIRC). And in this traceback, the object being serialized is a default value in a type schema, so there is no context.

davisagli avatar Mar 05 '23 17:03 davisagli