netbox-sync icon indicating copy to clipboard operation
netbox-sync copied to clipboard

fix: handle object and multi-object custom fields correctly

Open Littlericket opened this issue 9 months ago • 1 comments

Fix Custom Fields with object/multi-object type handling #451

Problem

When patching NetBox objects with custom fields of type "object" or "multi-object", netbox-sync tries to use the complete object representation received from the API instead of just the required object IDs. This causes errors like:

NetBox returned body: {'custom_fields': ["Cannot resolve keyword 'display' into field. [...]

The API returns object references in custom fields with properties like id, url, display, and name, but when patching, NetBox only expects the ID(s) of the referenced object(s).

Solution

Modified the update method in the NetBoxObject class to properly handle custom fields of type "object" and "multi-object". When patching values:

  • For "object" type fields: Extract only the ID from the object representation
  • For "multi-object" type fields: Extract an array of IDs from the object representations

Implementation Details

  1. Added "object" and "multi-object" to the list of supported types in the NBCustomField data model
  2. Implemented a pre-processing step for custom field values:
    • Added logic to determine the custom field type by querying the inventory
    • For "object" type fields: Convert {"id": 123, "display": "Object", ...} to just 123
    • For "multi-object" type fields: Convert array of objects to array of IDs
  3. Maintains backward compatibility with all other custom field types

Littlericket avatar Mar 21 '25 14:03 Littlericket

Thank you so much for the fix. Will have a look at it.

bb-Ricardo avatar Mar 22 '25 18:03 bb-Ricardo

Hi @bb-Ricardo , can you update on this? Theres a bunch of devices and VMs which arent "updating" or stopped updating because of this. For example, having a custom field with a linked object type will always fail after netbox-sync tries an PATCH.

Littlericket avatar Jun 25 '25 10:06 Littlericket