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

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

Open tiberiuichim opened this issue 2 years ago • 2 comments

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

tiberiuichim avatar Mar 23 '23 16:03 tiberiuichim

@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?

davisagli avatar Mar 23 '23 17:03 davisagli

@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()

tiberiuichim avatar Mar 23 '23 19:03 tiberiuichim