Fix duplicate validation errors for GenericIPAddressField with protocol#9982
Open
AsahiTakemura wants to merge 1 commit into
Open
Fix duplicate validation errors for GenericIPAddressField with protocol#9982AsahiTakemura wants to merge 1 commit into
AsahiTakemura wants to merge 1 commit 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
Fixes duplicated validation errors produced by DRF ModelSerializer when mapping Django GenericIPAddressField with an explicit protocol (IPv4/IPv6), by adjusting how model validators are carried over into serializer fields.
Changes:
- Filter out Django’s
validate_ipv4_address/validate_ipv6_addressvalidators (in addition tovalidate_ipv46_address) when building serializer field kwargs. - Add new
ModelSerializertests coveringGenericIPAddressField(protocol='IPv4')andprotocol='IPv6'.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
rest_framework/utils/field_mapping.py |
Extends the GenericIPAddressField validator filtering to avoid duplicated IP validation errors. |
tests/test_model_serializer.py |
Adds regression tests asserting a single validation error for protocol-specific GenericIPAddressField. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+443
to
+452
| class IPAddressFieldModel(models.Model): | ||
| address = models.GenericIPAddressField(protocol='IPv4') | ||
|
|
||
| class Meta: | ||
| app_label = 'test_model_serializer' | ||
|
|
||
| class TestSerializer(serializers.ModelSerializer): | ||
| class Meta: | ||
| model = IPAddressFieldModel | ||
| fields = '__all__' |
Comment on lines
+461
to
+470
| class IPAddressFieldModel(models.Model): | ||
| address = models.GenericIPAddressField(protocol='IPv6') | ||
|
|
||
| class Meta: | ||
| app_label = 'test_model_serializer' | ||
|
|
||
| class TestSerializer(serializers.ModelSerializer): | ||
| class Meta: | ||
| model = IPAddressFieldModel | ||
| fields = '__all__' |
Comment on lines
208
to
+212
| if isinstance(model_field, models.GenericIPAddressField): | ||
| validator_kwarg = [ | ||
| validator for validator in validator_kwarg | ||
| if validator is not validators.validate_ipv46_address | ||
| if validator not in ( | ||
| validators.validate_ipv46_address, |
Comment on lines
+454
to
+458
| 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)) |
Comment on lines
+472
to
+476
| 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)) |
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 #9645
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'), ]}Root Cause
Django adds a protocol-specific validator (
validate_ipv4_addressorvalidate_ipv6_address) to the model field. DRF'sIPAddressField.__init__also adds the same validator viaip_address_validators(). The existing code infield_mapping.pyonly removesvalidate_ipv46_address, leaving the protocol-specific validators in place, causing duplication.Fix
In
rest_framework/utils/field_mapping.py, extend the validator filter to also removevalidate_ipv4_addressandvalidate_ipv6_address:Test Plan
Added two new test cases:
test_ip_address_validation_with_protocol_ipv4— verifies only 1 error for invalid input withprotocol='IPv4'test_ip_address_validation_with_protocol_ipv6— verifies only 1 error for invalid input withprotocol='IPv6'All existing tests continue to pass.
🤖 Generated with Claude Code