mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-20 12:22:23 -06:00
feat(ipam): Normalize numeric ranges in API output
Adds logic to handle numeric range fields in API responses by converting them into inclusive `[low, high]` pairs for consistent behavior. Updates test cases with `vid_ranges` fields to reflect the changes. Closes #20491
This commit is contained in:
@@ -1071,14 +1071,17 @@ class VLANGroupTest(APIViewTestCases.APIViewTestCase):
|
|||||||
{
|
{
|
||||||
'name': 'VLAN Group 4',
|
'name': 'VLAN Group 4',
|
||||||
'slug': 'vlan-group-4',
|
'slug': 'vlan-group-4',
|
||||||
|
'vid_ranges': [[1, 4094]]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'name': 'VLAN Group 5',
|
'name': 'VLAN Group 5',
|
||||||
'slug': 'vlan-group-5',
|
'slug': 'vlan-group-5',
|
||||||
|
'vid_ranges': [[1, 4094]]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'name': 'VLAN Group 6',
|
'name': 'VLAN Group 6',
|
||||||
'slug': 'vlan-group-6',
|
'slug': 'vlan-group-6',
|
||||||
|
'vid_ranges': [[1, 4094]]
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
bulk_update_data = {
|
bulk_update_data = {
|
||||||
|
|||||||
@@ -141,8 +141,8 @@ class ModelTestCase(TestCase):
|
|||||||
elif value and type(field) is GenericForeignKey:
|
elif value and type(field) is GenericForeignKey:
|
||||||
model_dict[key] = value.pk
|
model_dict[key] = value.pk
|
||||||
|
|
||||||
|
# Handle API output
|
||||||
elif api:
|
elif api:
|
||||||
|
|
||||||
# Replace ContentType numeric IDs with <app_label>.<model>
|
# Replace ContentType numeric IDs with <app_label>.<model>
|
||||||
if type(getattr(instance, key)) in (ContentType, ObjectType):
|
if type(getattr(instance, key)) in (ContentType, ObjectType):
|
||||||
object_type = ObjectType.objects.get(pk=value)
|
object_type = ObjectType.objects.get(pk=value)
|
||||||
@@ -152,9 +152,13 @@ class ModelTestCase(TestCase):
|
|||||||
elif type(value) is IPNetwork:
|
elif type(value) is IPNetwork:
|
||||||
model_dict[key] = str(value)
|
model_dict[key] = str(value)
|
||||||
|
|
||||||
else:
|
# Normalize arrays of numeric ranges (e.g. VLAN IDs or port ranges).
|
||||||
field = instance._meta.get_field(key)
|
# DB uses canonical half-open [lo, hi) via NumericRange; API uses inclusive [lo, hi].
|
||||||
|
# Convert to inclusive pairs for stable API comparisons.
|
||||||
|
elif type(field) is ArrayField and issubclass(type(field.base_field), RangeField):
|
||||||
|
model_dict[key] = [[r.lower, r.upper - 1] for r in value]
|
||||||
|
|
||||||
|
else:
|
||||||
# Convert ArrayFields to CSV strings
|
# Convert ArrayFields to CSV strings
|
||||||
if type(field) is ArrayField:
|
if type(field) is ArrayField:
|
||||||
if getattr(field.base_field, 'choices', None):
|
if getattr(field.base_field, 'choices', None):
|
||||||
|
|||||||
Reference in New Issue
Block a user