Skip to content
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

Duplicate error msg fix for IP address field #9647

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Daksh2000
Copy link

@Daksh2000 Daksh2000 commented Feb 9, 2025

This PR fixes the issue of duplicate error message for GenericIPAddress Field in serializer
Issue - https://github.com/encode/django-rest-framework/issues/9645

Issue
-> While getting serializer field for corresponding model field and its attributes from get_field_kwargs() method,
the IP address field didn't take protocol defined from User , it assumed the default value by default, which was 'both'
-> Also, the validator for ip address are 3 as defined in [ip_address_validator_map] , but while getting corresponding
serializer field (for GenericIPAddress) validate_ipv46_address was only being considered.

Fix
-> Added protocol attribute for consideration and as well as took into account other vaidators as well, so that by default
validate_ipv46_address is not attached to validators.
Also set the default value of protocol to "both" as described in IPAddress Field

Copy link
Member

@browniebroke browniebroke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing some tests

@Daksh2000
Copy link
Author

Daksh2000 commented Feb 10, 2025

Missing some tests

Hi @browniebroke ,
I ran runtests.py as well as pytest , it seems to pass all the test cases, could you guide me as to what cases are missing , like do you mean tox here ?

@browniebroke
Copy link
Member

browniebroke commented Feb 10, 2025

  1. Write a new test that reproduces the problem and fails on the main branch
  2. Add your fix, makes the test pass

https://www.codecademy.com/article/tdd-red-green-refactor

@Daksh2000
Copy link
Author

  1. Write a new test that reproduces the problem and fails on the main branch
  2. Add your fix, makes the test pass

https://www.codecademy.com/article/tdd-red-green-refactor

Hi @browniebroke ,
Thanks for the guidance, I added the test cases accordingly , please share your views .

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to be a decent fix for a bug

Copy link
Member

@browniebroke browniebroke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on the tests, getting a good coverage of all the cases!



# Define the serializer in setUp
class TestSerializerTestCase(TestCase):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have expected this test to be next to the existin ones:

class TestGenericIPAddressFieldValidation(TestCase):

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out the existing test cases @browniebroke , I did some changes and incorporated my test cases within the same class, and did some minor changes along with it.

@tomchristie
Copy link
Member

Thank you @auvipy @browniebroke for your reviews.

How comfortable do we feel with allowing highly anon contributions. We've allowed it in the past, but we could take a different approach.

@Daksh2000
Copy link
Author

Thank you @auvipy @browniebroke for your reviews.

How comfortable do we feel with allowing highly anon contributions. We've allowed it in the past, but we could take a different approach.

Noted @tomchristie! , But could we at least fix this bug for now ?

@browniebroke
Copy link
Member

How comfortable do we feel with allowing highly anon contributions.

I don't have a strong opinion about that. I don't mind as long as the quality of the contribution passes our standard, and that they're respectful and considerate.

People have to start contributing somewhere and having just created a GitHub account shouldn't be a blocker. Not using their real name is a personal decision which, while I'm not being a big fan of, I respect.

Might be better to discuss this separately, I wonder if I'm missing anything... Is there a risk with doing so? How do we enforce it? What should be wary of?

@browniebroke
Copy link
Member

But could we at least fix this bug for now?

We'll get to it when we'll get to it. It looks good but there is no rush for merging it, as far as I'm concerned

@Daksh2000
Copy link
Author

But could we at least fix this bug for now?

We'll get to it when we'll get to it. It looks good but there is no rush for merging it, as far as I'm concerned

Sure @browniebroke , no worries :)

Comment on lines 447 to 459
def test_invalid_ipv6_for_ipv6_field(self):
"""Test that an invalid IPv6 raises only an IPv6-related error."""
self.model._meta.get_field("address").protocol = "IPv6" # Set field to IPv6 only
invalid_data = {"address": "invalid-ip"}
serializer = self.serializer_class(data=invalid_data)

with self.assertRaises(ValidationError) as context:
serializer.is_valid(raise_exception=True)

self.assertEqual(
str(context.exception.detail["address"][0]),
"Enter a valid IPv6 address."
)
Copy link
Member

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.

Copy link
Author

@Daksh2000 Daksh2000 Feb 14, 2025

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.
Screenshot 2025-02-14 at 11 26 35 PM

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.

Copy link
Author

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

@browniebroke browniebroke dismissed their stale review February 13, 2025 19:13

Request changes were addressed

@tomchristie
Copy link
Member

Naming isn't the issue, please do present however you wish. Just pointing out that the space can at times be overwhelmed with bot accounts and uninvested users demanding attention, and that it's okay for us forcefully weed that out. Let's make this a more inviting space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants