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

Provide a way to customize, per content type, the ISerializeToJsonSummary for IContentListingObject

Open tiberiuichim opened this issue 8 years ago • 6 comments

In SerializeCollectionToJson implementation for the collections, the result['items'] are ISerializeToJsonSummary of IContentListingObject.

Let's say I want to customize how the brains are serialized, but only some of them, for example, for the images, I want to add a "thumb" property. I would like it if the collection serialization code would do something like:

        results['items'] = []
        for brain in batch:
            adapter = queryMultiAdapter((brain, self.request),
                                        ISerializeToJsonSummary,
                                        name=brain.portal_type)
            if adapter is not None:
                results['items'].append(adapter())
                continue
            results['items'].append(
                getMultiAdapter((brain, self.request),
                                ISerializeToJsonSummary)()
                )

instead of the existing

        results['items'] = [
            getMultiAdapter((brain, self.request), ISerializeToJsonSummary)()
            for brain in batch
        ]

This would allow providing a custom json summary serializer, per different content type.

tiberiuichim avatar Apr 29 '17 15:04 tiberiuichim

~I know that it is possible to implement my use case by creating a metadata column in the catalog, with the thumb url value, but I believe this workaround is ugly.~ Scratch that, that's just to regular item serializing

tiberiuichim avatar Apr 29 '17 15:04 tiberiuichim

Hey, we're running into the same issue. The default serializer returns almost enough data when we request a folder containing a bunch of instances of a certain content type. If we could overwrite the serializer to include a tiny bit more data it would save us from having to make many extra http requests to get the details of each instance of the content type.

Does anyone know if a solution for this was ever implemented?

WestleyArgentum avatar Oct 25 '21 13:10 WestleyArgentum

Serializers are adapters and are defined here: https://github.com/plone/plone.restapi/tree/master/src/plone/restapi/serializer

If you need to serialize some other attributes, you need to write your own adapter and register it for your Interface of choice:

https://github.com/plone/plone.restapi/blob/master/src/plone/restapi/serializer/configure.zcml#L16 https://github.com/plone/plone.restapi/blob/master/src/plone/restapi/serializer/summary.py#L40

erral avatar Oct 25 '21 13:10 erral

@eral the problem is that that adapter is not taking the brain item into consideration, AFAIK. We want per brain customization

tiberiuichim avatar Oct 25 '21 14:10 tiberiuichim

OK then, per brain customization can't be achieved through this kind of adapters.

erral avatar Oct 25 '21 14:10 erral

Would it be sufficient to make the summary serializer leave out keys whose value in the catalog brain is Missing.Value, instead of serializing it as "key": null? Then you could add as many fields as you need, but they would only be included for the types that have a value in that column.

davisagli avatar Sep 05 '22 00:09 davisagli