flask-rest-jsonapi icon indicating copy to clipboard operation
flask-rest-jsonapi copied to clipboard

Querying of sparse fieldsets does not work

Open joe-at-siemens opened this issue 3 years ago • 1 comments

The 'only' attribute is set only after the schema has been instantiated in the compute_schema method, and so the set of attributes that are then returned in the dump method has already been determined.

joe-at-siemens avatar Sep 14 '21 12:09 joe-at-siemens

A workaround is to make a call to _init_fields() after schema.only is set:

if schema.opts.type_ in qs.fields:
        tmp_only = set(schema.declared_fields.keys()) & set(qs.fields[schema.opts.type_])
        if schema.only:
            tmp_only &= set(schema.only)
        schema.only = tuple(tmp_only)

        # make sure again that id field is in only parameter unless marshamllow will raise an Exception
        if schema.only is not None and 'id' not in schema.only:
            schema.only += ('id',)
        schema._init_fields()

joe-at-siemens avatar Sep 14 '21 13:09 joe-at-siemens