-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Duplicate error msg fix for IP address field #9647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Daksh2000
wants to merge
4
commits into
encode:master
Choose a base branch
from
Daksh2000:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3681824
Duplicate error msg fix for IP address field
dakshbhayana11811137 08aba54
WIP
dakshbhayana11811137 ec927c9
Refactored Test cases for GenericIPAddress Field
dakshbhayana11811137 6128db6
Refactored Test Cases with additional models and Serializers
dakshbhayana11811137 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to run this on our main branch and it doesn't raise the duplicated error messages as reported in the issue. The test fails, but it raises a single validation message (the generic one
Enter a valid IPv4 or IPv6 address.
).While it's a nice idea to try to share the test setup, I don't think it works very well. Basically, patching the field protocol on the model is not the same as defining a fresh model/serializer.
We should probably be a bit more exhaustive and define 3 models -or at least 3 separate fields- as I'd rather have these tests cover more realisticallyt how DRF is used in the wild.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @browniebroke
You're right! The issue was due to modifying protocol dynamically on an existing field, which doesn't work as expected. I've now defined 3 separate models (IPv4Model, IPv6Model, and BothProtocolsModel) and their corresponding serializers. This ensures the correct validation messages are raised for each protocol type. Thanks for catching this!
Summarizing the PR
Bug - When user creates a modelSerializers (for field models.GenericIPAddressField) , serializer.is_valid(raise_exception=True) gives 2 validation error messages for protocol - "IPv4" and "IPv6" (not for "both")
Fix - I tweaked few methods to account for 2 more validators - validate_ipv6_address and validate_ipv4_address along with their protocol.
This will give only one validation error for any protocol avoiding redundant error messages
Now as you pointed out that on your main branch when you tested it for ipv6 protocol, it gave you a generic message - "Enter a valid IPv4 or IPv6 address."

So I dug deeper and found out that when the data contains ":" (colon) in it, and has protocol either "both" or "IPv6" - [This maybe the case you could have encountered]
, the validations resorts to the generic error message only .
PFB the screenshot.
So for this case as well, I added 2 more test cases for it in order to have more branch coverage , I hope thats all right.
Please share your views.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @browniebroke , please let me know if any further changes are required