Fixes #21533: Fix missing family/mask_length in API when creating IP-related objects #259

Closed
opened 2026-04-05 16:23:50 +02:00 by MrUnknownDE · 0 comments
Owner

Originally created by @chbndrhnns on 3/2/2026

Fixes: #21533

This PR addresses an issue where read-only calculated fields (like family and mask_length) are silently omitted from API responses when creating IP-related objects (Prefix, IPAddress, Aggregate, IPRange).

Problem

During DRF's model instantiation and validation lifecycle, IP network fields are temporarily held as strings rather than netaddr objects. When the serializer attempts to build the response representation, evaluating properties like family raises an AttributeError (e.g., 'str' object has no attribute 'version'). DRF's Field.get_attribute() catches this exception for read-only fields and silently ignores it by raising a SkipField exception, dropping the key from the API response.

Solution

  • Modified the family and mask_length properties on IP-related models to detect if the underlying value is a string and cast it to a netaddr object before evaluating.
  • This ensures these properties can be safely evaluated at any point in the object's lifecycle, including during API validation and serialization.
  • Added unit tests in ipam.tests.test_models to explicitly verify that the models correctly parse the IP version and mask length when instantiated with raw string inputs.
*Originally created by @chbndrhnns on 3/2/2026* ### Fixes: #21533 This PR addresses an issue where read-only calculated fields (like `family` and `mask_length`) are silently omitted from API responses when creating IP-related objects (`Prefix`, `IPAddress`, `Aggregate`, `IPRange`). ### Problem During DRF's model instantiation and validation lifecycle, IP network fields are temporarily held as strings rather than `netaddr` objects. When the serializer attempts to build the response representation, evaluating properties like `family` raises an `AttributeError` (e.g., `'str' object has no attribute 'version'`). DRF's `Field.get_attribute()` catches this exception for read-only fields and silently ignores it by raising a `SkipField` exception, dropping the key from the API response. ### Solution - Modified the `family` and `mask_length` properties on IP-related models to detect if the underlying value is a string and cast it to a `netaddr` object before evaluating. - This ensures these properties can be safely evaluated at any point in the object's lifecycle, including during API validation and serialization. - Added unit tests in `ipam.tests.test_models` to explicitly verify that the models correctly parse the IP version and mask length when instantiated with raw string inputs.
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/netbox#259