flask-rest-jsonapi
flask-rest-jsonapi copied to clipboard
Querying of sparse fieldsets does not work
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.
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()