mirror of
https://github.com/encode/django-rest-framework.git
synced 2026-03-04 12:01:21 +03:00
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>
This commit is contained in:
parent
64bee7ff28
commit
1792a2fccc
|
|
@ -1,6 +1,7 @@
|
|||
from django.http import QueryDict
|
||||
|
||||
from rest_framework import serializers
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
|
||||
class TestSimpleBoundField:
|
||||
|
|
@ -211,6 +212,28 @@ class TestNestedBoundField:
|
|||
rendered_packed = ''.join(rendered.split())
|
||||
assert rendered_packed == expected_packed
|
||||
|
||||
def test_child_bound_field_after_parent_validation_error(self):
|
||||
class ChildSerializer(serializers.Serializer):
|
||||
value = serializers.CharField()
|
||||
|
||||
class ParentSerializer(serializers.Serializer):
|
||||
nested = ChildSerializer()
|
||||
|
||||
def validate_nested(self, nested):
|
||||
# Raise parent-level (non-field) validation error
|
||||
raise ValidationError(["parent-level nested error"])
|
||||
|
||||
serializer = ParentSerializer(data={"nested": {"value": "ignored"}})
|
||||
assert not serializer.is_valid()
|
||||
|
||||
# Parent-level error is a list (current problematic case)
|
||||
assert serializer.errors["nested"] == ["parent-level nested error"]
|
||||
parent_bound = serializer["nested"]
|
||||
child_bound = parent_bound["value"]
|
||||
assert child_bound.errors is None
|
||||
assert child_bound.value == "ignored"
|
||||
assert child_bound.name == "nested.value"
|
||||
|
||||
|
||||
class TestJSONBoundField:
|
||||
def test_as_form_fields(self):
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user