Skip to content

Commit cacad3c

Browse files
author
Ryo Chijiiwa
authored
Fix/links context bug (#292)
* fix bug where context not set in links endpoints
1 parent 4e2b01d commit cacad3c

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

dynamic_rest/fields/fields.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,13 @@ def get_serializer(self, *args, **kwargs):
251251
if self.embed and self._is_dynamic:
252252
init_args['embed'] = True
253253

254-
return self._get_cached_serializer(args, init_args)
254+
serializer = self._get_cached_serializer(args, init_args)
255+
serializer.parent = self
256+
return serializer
255257

256258
@resettable_cached_property
257259
def serializer(self):
258-
serializer = self.get_serializer()
259-
serializer.parent = self
260-
return serializer
260+
return self.get_serializer()
261261

262262
@cached_property
263263
def _is_dynamic(self):

dynamic_rest/viewsets.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,20 +391,22 @@ def list_related(self, request, pk=None, field_name=None):
391391
return Response("Not found", status=404)
392392

393393
# Serialize the related data. Use the field's serializer to ensure
394-
# it's configured identically to the sideload case.
395-
serializer = field.get_serializer(envelope=True)
394+
# it's configured identically to the sideload case. One difference
395+
# is we need to set `envelope=True` to get the sideload-processor
396+
# applied.
397+
related_szr = field.get_serializer(envelope=True)
396398
try:
397399
# TODO(ryo): Probably should use field.get_attribute() but that
398400
# seems to break a bunch of things. Investigate later.
399-
serializer.instance = getattr(obj, field.source)
401+
related_szr.instance = getattr(obj, field.source)
400402
except ObjectDoesNotExist:
401403
# See:
402404
# http://jsonapi.org/format/#fetching-relationships-responses-404
403405
# This is a case where the "link URL exists but the relationship
404406
# is empty" and therefore must return a 200.
405407
return Response({}, status=200)
406408

407-
return Response(serializer.data)
409+
return Response(related_szr.data)
408410

409411
def get_extra_filters(self, request):
410412
# Override this method to enable addition of extra filters

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
NAME = 'dynamic-rest'
55
DESCRIPTION = 'Adds Dynamic API support to Django REST Framework.'
66
URL = 'http://github.com/AltSchool/dynamic-rest'
7-
VERSION = '1.9.6'
7+
VERSION = '1.9.7'
88
SCRIPTS = ['manage.py']
99

1010
setup(

tests/serializers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ class Meta:
197197
favorite_pet = DynamicGenericRelationField(required=False)
198198

199199
def get_number_of_cats(self, user):
200+
if not self.context.get('request'):
201+
# Used in test_api.py::test_relation_includes_context
202+
raise Exception("No request object in context")
200203
location = user.location
201204
return len(location.cat_set.all()) if location else 0
202205

tests/test_api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,16 @@ def test_relation_includes(self):
11521152
content = json.loads(r.content.decode('utf-8'))
11531153
self.assertTrue('locations' in content)
11541154

1155+
def test_relation_includes_context(self):
1156+
r = self.client.get('/locations/1/users/?include[]=number_of_cats')
1157+
self.assertEqual(200, r.status_code)
1158+
1159+
# Note: the DynamicMethodField for `number_of_cats` checks to
1160+
# ensure context is set, and raises if not. If the request
1161+
# succeeded and `number_of_cats` is returned, it means that check
1162+
# passed.
1163+
self.assertTrue('number_of_cats' in r.data['users'][0])
1164+
11551165
def test_relation_excludes(self):
11561166
r = self.client.get('/locations/1/users/?exclude[]=location')
11571167
self.assertEqual(200, r.status_code)

0 commit comments

Comments
 (0)