Skip to content

Duplicated GenericIPAddressField validation errors with ModelSerializer and protocol #9645

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
3 of 4 tasks
HansAarneLiblik opened this issue Feb 3, 2025 · 4 comments
Open
3 of 4 tasks

Comments

@HansAarneLiblik
Copy link

HansAarneLiblik commented Feb 3, 2025

Hi!

I'm using GenericIPAddressField with explicit protocol as IPv4 (Also reproduces with IPv6) and ModelSerializer. If the input is not an valid IpAddress, DRF generates duplicate(ish) errors.

Repro

from django.db import models
from rest_framework.serializers import ModelSerializer


class A(models.Model):
    address = models.GenericIPAddressField(protocol="IPv4")  # Also reproduces with `IPv6`

    class Meta:
        app_label = "main"

class ASerializer(ModelSerializer[A]):
    class Meta:
        model = A
        fields = "__all__"


ser = ASerializer(data={'address': 'not an ip address'})
ser.is_valid(raise_exception=True)

Result

rest_framework.exceptions.ValidationError: {'address': [
    ErrorDetail(string='Enter a valid IPv4 address.', code='invalid'), 
    ErrorDetail(string='Enter a valid IPv4 or IPv6 address.', code='invalid')
]}

The second error message is incorrect

Thanks!

  • Raised initially as discussion - Can't, it gets deleted automatically - https://groups.google.com/g/django-rest-framework/c/eNNq1UYeww4
  • This is not a feature request suitable for implementation outside this project. Please elaborate what it is:
    • other type of bug fix
  • I have reduced the issue to the simplest possible case.
@Daksh2000
Copy link

Hi @HansAarneLiblik , Can I take this issue ?

@HansAarneLiblik
Copy link
Author

Sure, I don't mind!

@Daksh2000
Copy link

Hi @HansAarneLiblik ,
I have raised a PR , #9647
Please share your views on it.

@NikeGunn

This comment has been minimized.

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

No branches or pull requests

3 participants