Skip to content

Fix lost 0. index prefix for ListSerializer errors on DRF 3.18 - #122

Open
sshishov wants to merge 1 commit into
ghazi-git:mainfrom
sshishov:ss/fix-list-serializer-index-0-attr
Open

Fix lost 0. index prefix for ListSerializer errors on DRF 3.18#122
sshishov wants to merge 1 commit into
ghazi-git:mainfrom
sshishov:ss/fix-list-serializer-index-0-attr

Conversation

@sshishov

@sshishov sshishov commented Sep 2, 2026

Copy link
Copy Markdown

Summary

Fixes #121.

With DRF 3.18, ListSerializer.to_internal_value collects child errors in a dict keyed by the integer index of the item (e.g. {0: {"email": [...]}}) instead of a positional list as before. 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, an error on the first item (index 0) of a many=True payload lost its 0. prefix, while items at index 1 and above were prefixed correctly (any other int is truthy).

  • Before: ['email'] for an error on item 0, ['1.email'] for item 1
  • After: ['0.email'] for item 0, ['1.email'] for item 1

Fix

Check attr is not None instead of relying on attr's truthiness, so an attr of 0 is still treated as a valid prefix segment.

Test plan

  • Added test_list_serializer_errors_indexed_dict in tests/test_flatten_errors.py, covering the DRF 3.18-style index-keyed error dict.
  • Ran the full test suite locally with djangorestframework==3.18.0 — all 124 tests pass.
  • Verified against the exact reproduction script from the issue; output now matches the expected ['0.email'] / ['1.email'].

🤖 Generated with Claude Code

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ListSerializer (many=True): error on first item (index 0) loses its 0. prefix in attr with DRF 3.18

1 participant