Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tiberiuichim opened this issue Apr 29, 2017 · 6 comments

Comments

@tiberiuichim
Copy link
Contributor

tiberiuichim commented Apr 29, 2017

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
Copy link
Contributor Author

tiberiuichim commented Apr 29, 2017

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

@WestleyArgentum
Copy link

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?

@erral
Copy link
Sponsor Member

erral commented Oct 25, 2021

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

@tiberiuichim
Copy link
Contributor Author

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

@erral
Copy link
Sponsor Member

erral commented Oct 25, 2021

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

@davisagli
Copy link
Sponsor Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants