Skip to content

Commit c033db9

Browse files
knaperekscottfisk
authored andcommitted
Add option to specify default included resources (#250)
* Add option to specify default included resources Let's provide an extra JSONAPIMeta configuration option that specifies resources to be listed in the 'included' section of the response. These resources shall be included even if they're not explicitly mentioned in the 'include' request parameter. * Re-trigger Travis Build script The previous build failure was caused by some random environment issue. * Ignore default 'included' resources if specified in request Conform to the specs and ensure that no other resources are included in the response other than those explicitely requested inside the 'include' parameter in request. * Add tests for default included_resources feature
1 parent 2994592 commit c033db9

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

example/tests/integration/test_includes.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22
from django.core.urlresolvers import reverse
33

44
from example.tests.utils import load_json
5+
import mock
56

67
pytestmark = pytest.mark.django_db
78

89

9-
def test_included_data_on_list(multiple_entries, client):
10-
response = client.get(reverse("entry-list") + '?include=comments&page_size=5')
10+
11+
@mock.patch('rest_framework_json_api.utils.get_default_included_resources_from_serializer', new=lambda s: ['comments'])
12+
def test_default_included_data_on_list(multiple_entries, client):
13+
return test_included_data_on_list(multiple_entries=multiple_entries, client=client, query='?page_size=5')
14+
15+
16+
def test_included_data_on_list(multiple_entries, client, query='?include=comments&page_size=5'):
17+
response = client.get(reverse("entry-list") + query)
1118
included = load_json(response.content).get('included')
1219

1320
assert len(load_json(response.content)['data']) == len(multiple_entries), 'Incorrect entry count'
@@ -18,8 +25,13 @@ def test_included_data_on_list(multiple_entries, client):
1825
assert comment_count == expected_comment_count, 'List comment count is incorrect'
1926

2027

21-
def test_included_data_on_detail(single_entry, client):
22-
response = client.get(reverse("entry-detail", kwargs={'pk': single_entry.pk}) + '?include=comments')
28+
@mock.patch('rest_framework_json_api.utils.get_default_included_resources_from_serializer', new=lambda s: ['comments'])
29+
def test_default_included_data_on_detail(single_entry, client):
30+
return test_included_data_on_detail(single_entry=single_entry, client=client, query='')
31+
32+
33+
def test_included_data_on_detail(single_entry, client, query='?include=comments'):
34+
response = client.get(reverse("entry-detail", kwargs={'pk': single_entry.pk}) + query)
2335
included = load_json(response.content).get('included')
2436

2537
assert [x.get('type') for x in included] == ['comments'], 'Detail included types are incorrect'

requirements-development.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pytest-django
44
pytest-factoryboy
55
fake-factory
66
tox
7+
mock

rest_framework_json_api/renderers.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -415,12 +415,6 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
415415
if resource_name == 'errors':
416416
return self.render_errors(data, accepted_media_type, renderer_context)
417417

418-
include_resources_param = request.query_params.get('include') if request else None
419-
if include_resources_param:
420-
included_resources = include_resources_param.split(',')
421-
else:
422-
included_resources = list()
423-
424418
json_api_data = data
425419
json_api_included = list()
426420
# initialize json_api_meta with pagination meta or an empty dict
@@ -433,6 +427,13 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
433427

434428
serializer = getattr(serializer_data, 'serializer', None)
435429

430+
# Build a list of included resources
431+
include_resources_param = request.query_params.get('include') if request else None
432+
if include_resources_param:
433+
included_resources = include_resources_param.split(',')
434+
else:
435+
included_resources = utils.get_default_included_resources_from_serializer(serializer)
436+
436437
if serializer is not None:
437438

438439
# Get the serializer fields

rest_framework_json_api/utils.py

+7
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ def get_resource_type_from_serializer(serializer):
232232
return get_resource_type_from_model(serializer.Meta.model)
233233

234234

235+
def get_default_included_resources_from_serializer(serializer):
236+
try:
237+
return list(serializer.JSONAPIMeta.included_resources)
238+
except AttributeError:
239+
return []
240+
241+
235242
def get_included_serializers(serializer):
236243
included_serializers = copy.copy(getattr(serializer, 'included_serializers', dict()))
237244

0 commit comments

Comments
 (0)