Fix lost 0. index prefix for ListSerializer errors on DRF 3.18 - #122
Open
sshishov wants to merge 1 commit into
Open
Fix lost 0. index prefix for ListSerializer errors on DRF 3.18#122sshishov wants to merge 1 commit into
sshishov wants to merge 1 commit into
Conversation
Since DRF 3.18, ListSerializer.to_internal_value collects child
validation errors in a dict keyed by the integer index of the item
(e.g. {0: {"email": [...]}}) instead of a positional list. In
flatten_errors, the dict branch only prefixed a child key with the
parent attr when `if attr:` was truthy. Since the integer 0 is
falsy in Python, errors on the first item of a many=True payload
lost their "0." prefix, while items at index 1 and above were
prefixed correctly (any other int is truthy).
Fix by checking `attr is not None` instead of relying on the
truthiness of attr, so an attr of 0 is still treated as a valid
prefix segment.
Fixes ghazi-git#121
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #121.
With DRF 3.18,
ListSerializer.to_internal_valuecollects child errors in a dict keyed by the integer index of the item (e.g.{0: {"email": [...]}}) instead of a positional list as before. Inflatten_errors, the dict branch only prefixed a child key with the parentattrwhenif attr:was truthy. Since the integer0is falsy in Python, an error on the first item (index0) of amany=Truepayload lost its0.prefix, while items at index1and above were prefixed correctly (any other int is truthy).['email']for an error on item 0,['1.email']for item 1['0.email']for item 0,['1.email']for item 1Fix
Check
attr is not Noneinstead of relying onattr's truthiness, so anattrof0is still treated as a valid prefix segment.Test plan
test_list_serializer_errors_indexed_dictintests/test_flatten_errors.py, covering the DRF 3.18-style index-keyed error dict.djangorestframework==3.18.0— all 124 tests pass.['0.email']/['1.email'].🤖 Generated with Claude Code