plone / plone.restapi

RESTful API for Plone.
http://plonerestapi.readthedocs.org/
84 stars 73 forks source link

Adaptor implementations should be generic, they should not directly read request.form #1604

Open tiberiuichim opened 1 year ago

tiberiuichim commented 1 year ago

An adapter should be a reusable piece of infrastructure. Its implementation should be as generic as possible. But here, and in many other places, we have the adaptors directly reading the request form. This makes them difficult to use. It's the equivalent of depending on a single global variable (the request.form) that's used across the whole system. That's not flexible.

https://github.com/plone/plone.restapi/blob/b15ddc03e1add4197458f8ba55551e62a0d58b3d/src/plone/restapi/serializer/summary.py#L80

davisagli commented 1 year ago

@tiberiuichim I'm not sure I understand the context here. Let's focus on the specific use case. What are you trying to do with the summary serializer or change about it that is currently difficult?

tiberiuichim commented 1 year ago

@davisagli

I have a service (exposed as an expander) where I want to expose some serialized brains. It's "adjacent information", not strictly related to the default content that's serialized.

I want to serialize those brains with their full metadata. I can't do:

serializer = getMultiAdapter((brain, request), ISerializeToJsonSummary)

because there's no way of passing any option to that adaptor. Instead, I have to do:

class FakeRequest:
    def __init__(self, form):
        self.form = form

    def set(self, k, value):
        self.form[k] = value

    def get(self, k, default=None):
        return self.form.get(k, default)

def tojson(brain):
    form = {"metadata_fields": "_all"}
    request = FakeRequest(form)
    serializer = getMultiAdapter((brain, request), ISerializeToJsonSummary)
    return serializer()