Skip to content

Fix duplicate validation errors for GenericIPAddressField with protocol - #10011

Open
browniebroke wants to merge 7 commits into
encode:mainfrom
browniebroke:fix/duplicate-ip-validation
Open

Fix duplicate validation errors for GenericIPAddressField with protocol#10011
browniebroke wants to merge 7 commits into
encode:mainfrom
browniebroke:fix/duplicate-ip-validation

Conversation

@browniebroke

@browniebroke browniebroke commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

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') or GenericIPAddressField(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

justyou0606 and others added 3 commits June 14, 2026 10:27
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

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 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 protocol through get_field_kwargs() when mapping GenericIPAddressFieldIPAddressField.
  • 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' and protocol='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.

Comment thread tests/test_model_serializer.py Outdated

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

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

Comment thread rest_framework/utils/field_mapping.py

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@browniebroke browniebroke added this to the 3.18 milestone Aug 6, 2026

@auvipy auvipy 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.

some python build are failing...

@browniebroke

Copy link
Copy Markdown
Collaborator Author

some python build are failing...

Yes, looks like GHA is having a moment... 😮‍💨

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.

Duplicated GenericIPAddressField validation errors with ModelSerializer and protocol

3 participants