Skip to content

Commit 1792a2f

Browse files
Shrikantgiri25browniebrokeauvipy
authored
Add test to cover nested serializer with parent-level ValidationError (#9854)
Co-authored-by: Bruno Alla <browniebroke@users.noreply.github.com> Co-authored-by: Asif Saif Uddin {"Auvi":"অভি"} <auvipy@gmail.com> Co-authored-by: Bruno Alla <alla.brunoo@gmail.com>
1 parent 64bee7f commit 1792a2f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

tests/test_bound_fields.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.http import QueryDict
22

33
from rest_framework import serializers
4+
from rest_framework.exceptions import ValidationError
45

56

67
class TestSimpleBoundField:
@@ -211,6 +212,28 @@ class ExampleSerializer(serializers.Serializer):
211212
rendered_packed = ''.join(rendered.split())
212213
assert rendered_packed == expected_packed
213214

215+
def test_child_bound_field_after_parent_validation_error(self):
216+
class ChildSerializer(serializers.Serializer):
217+
value = serializers.CharField()
218+
219+
class ParentSerializer(serializers.Serializer):
220+
nested = ChildSerializer()
221+
222+
def validate_nested(self, nested):
223+
# Raise parent-level (non-field) validation error
224+
raise ValidationError(["parent-level nested error"])
225+
226+
serializer = ParentSerializer(data={"nested": {"value": "ignored"}})
227+
assert not serializer.is_valid()
228+
229+
# Parent-level error is a list (current problematic case)
230+
assert serializer.errors["nested"] == ["parent-level nested error"]
231+
parent_bound = serializer["nested"]
232+
child_bound = parent_bound["value"]
233+
assert child_bound.errors is None
234+
assert child_bound.value == "ignored"
235+
assert child_bound.name == "nested.value"
236+
214237

215238
class TestJSONBoundField:
216239
def test_as_form_fields(self):

0 commit comments

Comments
 (0)