Skip to content

Commit

Permalink
12278 add ipaddressfield serializer for OpenAPI spectacular typing (#…
Browse files Browse the repository at this point in the history
…12285)

* #12278 add serializer for ipaddressfield to remove spectacular warnings

* #12278 add ipaddressfieldserializer to nested serializers

* #12278 fix to_internal_value to_representation in serializer

* #12278 to_internal_value is called before validation! need to raise validation error if incorrect format

* #12278 to_internal_value needs to return value doh

* #12278 move IPAddressField to field_serializers

* #12278 remove old import

* 12278 remove validator
  • Loading branch information
arthanson authored Apr 21, 2023
1 parent 15d80f4 commit 55385dd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
32 changes: 32 additions & 0 deletions netbox/ipam/api/field_serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers

from ipam import models
from netaddr import AddrFormatError, IPNetwork

__all__ = [
'IPAddressField',
]


#
# IP address field
#

class IPAddressField(serializers.CharField):
"""IPAddressField with mask"""

default_error_messages = {
'invalid': _('Enter a valid IPv4 or IPv6 address with optional mask.'),
}

def to_internal_value(self, data):
try:
return IPNetwork(data)
except AddrFormatError:
raise serializers.ValidationError("Invalid IP address format: {}".format(data))
except (TypeError, ValueError) as e:
raise serializers.ValidationError(e)

def to_representation(self, value):
return str(value)
4 changes: 4 additions & 0 deletions netbox/ipam/api/nested_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ipam import models
from ipam.models.l2vpn import L2VPNTermination, L2VPN
from netbox.api.serializers import WritableNestedSerializer
from .field_serializers import IPAddressField

__all__ = [
'NestedAggregateSerializer',
Expand Down Expand Up @@ -182,6 +183,8 @@ class Meta:
class NestedIPRangeSerializer(WritableNestedSerializer):
url = serializers.HyperlinkedIdentityField(view_name='ipam-api:iprange-detail')
family = serializers.IntegerField(read_only=True)
start_address = IPAddressField()
end_address = IPAddressField()

class Meta:
model = models.IPRange
Expand All @@ -195,6 +198,7 @@ class Meta:
class NestedIPAddressSerializer(WritableNestedSerializer):
url = serializers.HyperlinkedIdentityField(view_name='ipam-api:ipaddress-detail')
family = serializers.IntegerField(read_only=True)
address = IPAddressField()

class Meta:
model = models.IPAddress
Expand Down
4 changes: 4 additions & 0 deletions netbox/ipam/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from utilities.api import get_serializer_for_model
from virtualization.api.nested_serializers import NestedVirtualMachineSerializer
from .nested_serializers import *
from .field_serializers import IPAddressField


#
Expand Down Expand Up @@ -369,6 +370,8 @@ def to_representation(self, instance):
class IPRangeSerializer(NetBoxModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='ipam-api:iprange-detail')
family = ChoiceField(choices=IPAddressFamilyChoices, read_only=True)
start_address = IPAddressField()
end_address = IPAddressField()
vrf = NestedVRFSerializer(required=False, allow_null=True)
tenant = NestedTenantSerializer(required=False, allow_null=True)
status = ChoiceField(choices=IPRangeStatusChoices, required=False)
Expand All @@ -391,6 +394,7 @@ class Meta:
class IPAddressSerializer(NetBoxModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='ipam-api:ipaddress-detail')
family = ChoiceField(choices=IPAddressFamilyChoices, read_only=True)
address = IPAddressField()
vrf = NestedVRFSerializer(required=False, allow_null=True)
tenant = NestedTenantSerializer(required=False, allow_null=True)
status = ChoiceField(choices=IPAddressStatusChoices, required=False)
Expand Down

0 comments on commit 55385dd

Please sign in to comment.