Skip to content

Commit 26ebb88

Browse files
Ryan P Kilbytomchristie
authored andcommitted
Revert 3288 (#5313)
* Add regression test for #2505. Thanks @pySilver! * Add regression test for #5087 * Revert "Cached the field's root and context property." This reverts commit 7920058.
1 parent 21074a0 commit 26ebb88

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

rest_framework/fields.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from django.utils.duration import duration_string
2727
from django.utils.encoding import is_protected_type, smart_text
2828
from django.utils.formats import localize_input, sanitize_separators
29-
from django.utils.functional import cached_property
3029
from django.utils.ipv6 import clean_ipv6_address
3130
from django.utils.timezone import utc
3231
from django.utils.translation import ugettext_lazy as _
@@ -586,7 +585,7 @@ def fail(self, key, **kwargs):
586585
message_string = msg.format(**kwargs)
587586
raise ValidationError(message_string, code=key)
588587

589-
@cached_property
588+
@property
590589
def root(self):
591590
"""
592591
Returns the top-level serializer for this field.
@@ -596,7 +595,7 @@ def root(self):
596595
root = root.parent
597596
return root
598597

599-
@cached_property
598+
@property
600599
def context(self):
601600
"""
602601
Returns the context as passed to the root serializer on initialization.

tests/test_fields.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,16 @@ class TestSerializer(serializers.Serializer):
502502
assert serializer.validated_data['context_set'] == 'success'
503503

504504

505+
class Test5087Regression:
506+
def test_parent_binding(self):
507+
parent = serializers.Serializer()
508+
field = serializers.CharField()
509+
510+
assert field.root is field
511+
field.bind('name', parent)
512+
assert field.root is parent
513+
514+
505515
# Tests for field input and output values.
506516
# ----------------------------------------
507517

tests/test_serializer.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,22 @@ def test_validation_success(self):
469469
assert serializer.errors == {}
470470

471471

472+
class Test2505Regression:
473+
def test_serializer_context(self):
474+
class NestedSerializer(serializers.Serializer):
475+
def __init__(self, *args, **kwargs):
476+
super(NestedSerializer, self).__init__(*args, **kwargs)
477+
# .context should not cache
478+
self.context
479+
480+
class ParentSerializer(serializers.Serializer):
481+
nested = NestedSerializer()
482+
483+
serializer = ParentSerializer(data={}, context={'foo': 'bar'})
484+
assert serializer.context == {'foo': 'bar'}
485+
assert serializer.fields['nested'].context == {'foo': 'bar'}
486+
487+
472488
class Test4606Regression:
473489
def setup(self):
474490
class ExampleSerializer(serializers.Serializer):

0 commit comments

Comments
 (0)