Repository: miLibris/flask-rest-jsonapi

Issue:
Propagate marshmallow context to included schemas
According to [marshmallow documentation](https://marshmallow.readthedocs.io/en/3.0/custom_fields.html#adding-context-to-method-and-function-fields) it is  possible to add `context` to a schema.

It's currently easy to add `context` via `flask-rest-jsonapi` using:
```
class GeokretDetail(ResourceDetail):
    schema = GeokretSchemaPublic
    get_schema_kwargs = {'context': {'current_identity': current_identity}}
    […]
```

However, the current implementation doesn't propagate context to included resources.

Discussion:
coveralls: 
[![Coverage Status](https://coveralls.io/builds/18787507/badge)](https://coveralls.io/builds/18787507)

Coverage decreased (-0.2%) to 93.503% when pulling **d21f60ada81f2fd488323f43d3d6d99f567eaedd on geokrety:propagate-context** into **268e94808573033e0232007a887982207f715e9a on miLibris:master**.


Patch:
diff --git a/.travis.yml b/.travis.yml
index 15f8627..99c8a3f 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -8,6 +8,7 @@ python:
 install:
   - pip install -r requirements.txt
   - pip install coveralls coverage
+  - pip install -U setuptools
   - pip install pytest --upgrade
 script:
   - python setup.py install
diff --git a/flask_rest_jsonapi/schema.py b/flask_rest_jsonapi/schema.py
index 8321abe..22f0bde 100644
--- a/flask_rest_jsonapi/schema.py
+++ b/flask_rest_jsonapi/schema.py
@@ -66,6 +66,8 @@ def compute_schema(schema_cls, default_kwargs, qs, include):
             relation_field = schema.declared_fields[field]
             related_schema_cls = schema.declared_fields[field].__dict__['_Relationship__schema']
             related_schema_kwargs = {}
+            if 'context' in default_kwargs:
+                related_schema_kwargs['context'] = default_kwargs['context']
             if isinstance(related_schema_cls, SchemaABC):
                 related_schema_kwargs['many'] = related_schema_cls.many
                 related_schema_cls = related_schema_cls.__class__


Test patch:
diff --git a/tests/test_sqlalchemy_data_layer.py b/tests/test_sqlalchemy_data_layer.py
index d5f62a3..e33ed13 100644
--- a/tests/test_sqlalchemy_data_layer.py
+++ b/tests/test_sqlalchemy_data_layer.py
@@ -433,6 +433,15 @@ def test_compute_schema(person_schema):
     flask_rest_jsonapi.schema.compute_schema(person_schema, dict(only=list()), qsm, list())
 
 
+def test_compute_schema_propagate_context(person_schema, computer_schema):
+    query_string = {}
+    qsm = QSManager(query_string, person_schema)
+    schema = flask_rest_jsonapi.schema.compute_schema(person_schema, dict(), qsm, ['computers'])
+    assert schema.declared_fields['computers'].__dict__['_Relationship__schema'].__dict__['context'] == dict()
+    schema = flask_rest_jsonapi.schema.compute_schema(person_schema, dict(context=dict(foo='bar')), qsm, ['computers'])
+    assert schema.declared_fields['computers'].__dict__['_Relationship__schema'].__dict__['context'] == dict(foo='bar')
+
+
 # test good cases
 def test_get_list(client, register_routes, person, person_2):
     with client: