Skip to content

Commit

Permalink
Fix using multiple includes with sub-schemas, fixes miLibris#56 and m…
Browse files Browse the repository at this point in the history
…iLibris#29.

Due to the layout of the loop creating the related_schema isntances, all
but the last related_include were lost. They are thus now collected in
the inital step where all include_paths are evaluated, so that the
schema creation code can use the colelcted sub-includes later.

It is still not perfect, because the related_schema is created multiple
times and later discarded in some cases, but that's a minor issue and
was there before as well. The important thing is that the last schema
created is complete now.
  • Loading branch information
Natureshadow committed Jul 21, 2017
1 parent 9566f6f commit a000bc7
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions flask_rest_jsonapi/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def compute_schema(schema_cls, default_kwargs, qs, include):
schema_kwargs = default_kwargs
schema_kwargs['include_data'] = tuple()

# collect sub-related_includes
related_includes = {}

if include:
for include_path in include:
field = include_path.split('.')[0]
Expand All @@ -29,6 +32,10 @@ def compute_schema(schema_cls, default_kwargs, qs, include):
elif not isinstance(schema_cls._declared_fields[field], Relationship):
raise InvalidInclude("{} is not a relationship attribute of {}".format(field, schema_cls.__name__))
schema_kwargs['include_data'] += (field, )
if field not in related_includes:
related_includes[field] = []
if '.' in include_path:
related_includes[field] += ['.'.join(include_path.split('.')[1:])]

# make sure id field is in only parameter unless marshamllow will raise an Exception
if schema_kwargs.get('only') is not None and 'id' not in schema_kwargs['only']:
Expand Down Expand Up @@ -65,11 +72,7 @@ def compute_schema(schema_cls, default_kwargs, qs, include):
related_schema_cls = related_schema_cls.__class__
if isinstance(related_schema_cls, str):
related_schema_cls = class_registry.get_class(related_schema_cls)
if '.' in include_path:
related_include = ['.'.join(include_path.split('.')[1:])]
else:
related_include = None
related_schema = compute_schema(related_schema_cls, related_schema_kwargs, qs, related_include)
related_schema = compute_schema(related_schema_cls, related_schema_kwargs, qs, related_includes[field] or None)
relation_field.__dict__['_Relationship__schema'] = related_schema

return schema
Expand Down

0 comments on commit a000bc7

Please sign in to comment.