Registration of plone.restapi converter adapters together with collective.exportimport

I have a customer exporter for Plone 4.3 (based on collective.exportimport) that throws the following
warning for content items:


2023-09-15 10:02:02 ERROR collective.exportimport.export_content Error exporting http://nohost/magazine/segnalazioni/unibocultura/tutto-il-verde-intorno-a-noi
Traceback (most recent call last):
  File "/home/ajung/src/unibo/eggs/collective.exportimport-1.9-py2.7.egg/collective/exportimport/export_content.py", line 311, in export_content
    item = serializer()
  File "/home/ajung/src/unibo/eggs/plone.restapi-7.8.2-py2.7.egg/plone/restapi/serializer/dxcontent.py", line 98, in __call__
    value = serializer()
  File "/home/ajung/src/unibo/eggs/collective.exportimport-1.9-py2.7.egg/collective/exportimport/serializer.py", line 165, in __call__
    return json_compatible(value)
  File "/home/ajung/src/unibo/eggs/plone.restapi-7.8.2-py2.7.egg/plone/restapi/serializer/converters.py", line 72, in json_compatible
    return IJsonCompatible(value, None)
  File "/home/ajung/src/unibo/eggs/zope.component-3.9.5-py2.7.egg/zope/component/hooks.py", line 104, in adapter_hook
    return siteinfo.adapter_hook(interface, object, name, default)
  File "/home/ajung/src/unibo/eggs/plone.restapi-7.8.2-py2.7.egg/plone/restapi/serializer/converters.py", line 105, in list_converter
    return list(map(json_compatible, value))
  File "/home/ajung/src/unibo/eggs/plone.restapi-7.8.2-py2.7.egg/plone/restapi/serializer/converters.py", line 72, in json_compatible
    return IJsonCompatible(value, None)
  File "/home/ajung/src/unibo/eggs/zope.component-3.9.5-py2.7.egg/zope/component/hooks.py", line 104, in adapter_hook
    return siteinfo.adapter_hook(interface, object, name, default)
  File "/home/ajung/src/unibo/eggs/plone.restapi-7.8.2-py2.7.egg/plone/restapi/serializer/converters.py", line 86, in default_converter
    " {0!r} ({1}) JSON compatible.".format(value, type(value))
TypeError: No converter for making <SottoeventoUnibocultura Visite guidate all'Orto Botanico ed Erbario> (<class 'unibo.cultura.contents.sottoevento.SottoeventoUnibocultura'>) JSON compatible.

So I added the following adapter to my export script

@adapter(SottoeventoUnibocultura)
@implementer(IJsonCompatible)
def sottoevento_converter(value):
    print("x"*800)
    import pdb; pdb.set_trace()
    if value is None:
        return value

However, the registration seems to be ignored (the warning stays the same).

Is there anything missing here? Wild guess: the code registration must happen after calling the setSite() hook?

I assume, you have registered the adapter in zcml and included the plone.restapi package before:

<include package="plone.restapi" />
<adapter factory=".converters.sottoevento_converter" />

restapi version 7.8.2 is a bit old. 8.43.2 is the current one for plone 6. why that old version?

1 Like

plone.restapi 8 does not run on the source Plone 4.3 system.

Is the explicit <adapter> registration through ZCML really needed?
Aren't the decorators supposed to perform the registration on the fly during startup?

Afaik you cannot (reliable) register your adapter only in the code ... you need to import your module with the registration somewhere and take care of the imports and (site)hooks setup yourself. zcml does this for you and by including packages before your registration its ensured to have the hooks you need.

The point is that migration scripts - in this case the exporter script - is not part of any regular Plone add-on code. Such scripts are usually started through bin/instance run exporter.py.

I usually register my exporter scripts as BrowserView as outlined here GitHub - collective/collective.exportimport: Export and import content and other data from and to Plone and run it with curl on the console.

Using an explicit adapter registration with ZCML actually helped and solved the problem.
Thanks!