Fix duplicate validation errors for GenericIPAddressField with protocol - #10011
Open
browniebroke wants to merge 7 commits into
Open
Fix duplicate validation errors for GenericIPAddressField with protocol#10011browniebroke wants to merge 7 commits into
browniebroke wants to merge 7 commits into
Conversation
When a Django model uses GenericIPAddressField(protocol='IPv4') or (protocol='IPv6'), the model field gets a protocol-specific validator (validate_ipv4_address or validate_ipv6_address) from Django. DRF's IPAddressField also adds the same validator via ip_address_validators(), resulting in duplicate error messages. Fix: Remove validate_ipv4_address and validate_ipv6_address from the serializer's validator_kwarg, in addition to the already-removed validate_ipv46_address. Fixes encode#9645
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses duplicate validation errors produced by ModelSerializer when mapping Django GenericIPAddressField(protocol='IPv4'/'IPv6') to DRF’s IPAddressField, ensuring only the protocol-relevant validator/message is used.
Changes:
- Pass the model field’s
protocolthroughget_field_kwargs()when mappingGenericIPAddressField→IPAddressField. - Filter out Django’s IP address validators (
validate_ipv46_address,validate_ipv4_address,validate_ipv6_address) to prevent duplicate validation. - Add regression tests for
protocol='IPv4'andprotocol='IPv6'mappings and validation behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
rest_framework/utils/field_mapping.py |
Propagates protocol and removes protocol-related Django validators to avoid duplicate errors. |
tests/test_model_serializer.py |
Adds tests for protocol-specific mapping/validation on GenericIPAddressField. |
Suppressed comments (1)
tests/test_model_serializer.py:494
- As above, this only asserts the number of errors. To ensure the duplicate-validator fix is actually returning the protocol-specific error (and not leaving the generic message), assert the expected message for an IPv6-only field.
s = TestSerializer(data={'address': 'not an ip address'})
self.assertFalse(s.is_valid())
self.assertEqual(1, len(s.errors['address']),
'Unexpected number of validation errors: '
'{}'.format(s.errors))
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
auvipy
reviewed
Aug 6, 2026
auvipy
left a comment
Collaborator
There was a problem hiding this comment.
some python build are failing...
Collaborator
Author
Yes, looks like GHA is having a moment... 😮💨 |
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.
Description
Adding follow up fixes on top of:
Since we can't push them and the contributor has become unresponsive...
When a Django model uses
GenericIPAddressField(protocol='IPv4')orGenericIPAddressField(protocol='IPv6'), the serializer generates duplicate validation errors for invalid input:{'address': [ ErrorDetail(string='Enter a valid IPv4 address.', code='invalid'), ErrorDetail(string='Enter a valid IPv4 or IPv6 address.', code='invalid'), ]}After this fix, only the relevant error message is returned, depending on the accepted protocols by the field.
Fixes #9645
Closes #9647
Closes #9982