Skip to content

Add test to cover nested serializer with parent-level ValidationError - #9854

Merged
browniebroke merged 6 commits into
encode:mainfrom
Shrikantgiri25:fix-4073-nested-boundfield-errors
Feb 24, 2026
Merged

browniebroke merged 6 commits into
encode:mainfrom
Shrikantgiri25:fix-4073-nested-boundfield-errors

Conversation

@Shrikantgiri25

@Shrikantgiri25 Shrikantgiri25 commented Dec 19, 2025 •

Copy link
Copy Markdown
Contributor

When a nested serializer raises a parent-level ValidationError, the resulting
error value is a list rather than a mapping. Accessing child bound fields in this
case causes NestedBoundField.__getitem__() to pass None for errors, breaking
the bound-field contract.

This change normalizes list-based errors to an empty dict when resolving child
bound fields, ensuring they remain accessible.

The problem was reported in #4073 but has since been fixed. This PR adds a test to cover the case that caused the error.

Closes #4073

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where accessing child bound fields fails after a parent-level validation error in nested serializers. When a nested serializer raises a parent-level ValidationError (resulting in a list rather than a dict), the NestedBoundField.__getitem__() method was passing None for errors, breaking the bound-field contract.

Key changes:

  • Normalizes list-based errors to an empty dict when resolving child bound fields
  • Adds regression test for issue #4073

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
rest_framework/utils/serializer_helpers.py Updated NestedBoundField.__getitem__() to handle list-based errors by normalizing them to an empty dict
tests/test_bound_fields.py Added regression test verifying child bound fields remain accessible after parent-level validation errors

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_bound_fields.py Resolved
@Natgho

Natgho commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

I checked the ticket and PR, I think it's reasonable and LGTM. @browniebroke

@browniebroke browniebroke left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need any changes to the DRF code that we release (in rest_framework/utils/serializer_helpers.py) but the added test coverage would be welcome.

Could you please:

  • Revert the changes from rest_framework/utils/serializer_helpers.py
  • Update the test to test that child_bound.errors is None in this case

The rest can probably can stay as is

Comment thread tests/test_bound_fields.py Resolved
Comment thread tests/test_bound_fields.py Resolved
@browniebroke browniebroke changed the title Fix NestedBoundField child access after parent validation error Add test to cover nested serializer with parent-level ValidationError Feb 24, 2026
@browniebroke
browniebroke merged commit 1792a2f into encode:main Feb 24, 2026
7 checks passed
@browniebroke browniebroke added this to the 3.17 milestone Feb 24, 2026
@Shrikantgiri25

Copy link
Copy Markdown
Contributor Author

Thanks @browniebroke and @auvipy ! Appreciate the small test improvements and for merging this.
The issue might be limited to older versions, but I’m glad the tests were accepted. 🙌
Thanks as well to @Natgho for taking the time to review.

HosseinSayyedMousavi pushed a commit to HosseinSayyedMousavi/django-rest-framework that referenced this pull request Oct 8, 2026
…r` (encode#9854)

Co-authored-by: Bruno Alla <[email protected]>
Co-authored-by: Asif Saif Uddin {"Auvi":"অভি"} <[email protected]>
Co-authored-by: Bruno Alla <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

Errors from parent-level validation of nested serializer fields break the browsable API.

5 participants