Skip to content
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

Fix cable serialize #662

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

srfwx
Copy link
Contributor

@srfwx srfwx commented Dec 9, 2024

Fixes: #555

The way pynetbox currently handles mixed lists doesn't allow to re-instantiate the proper nested object types when loading values from a serialized() object. This is because the generic_list_parser "gobbles" that information by construction specific Record types from the mixed list.

Example:

>>> print(cable.serialize())

{'a_terminations': [530082],
 'b_terminations': [314260],
 'color': '',
 'comments': '',
 'created': '2022-05-03T02:00:00+02:00',
 'custom_fields': {},
 'description': '',
 'display': '1001',
 'id': 155652,
 'label': '1001',
 'last_updated': '2022-06-29T14:36:49.386886+02:00',
 'length': None,
 'length_unit': None,
 'status': 'connected',
 'tags': [],
 'tenant': 2510,
 'type': '',
 'url': 'https://localhost:8000/api/dcim/cables/155652/'}

Patching the record a_terminations / b_terminations with new ID wouldn't work as with other nested Record types as in this case the API expects a dict with object_type and object_id keys.

I propose to solve this by introducing a GenericListItem class that more accurately stores the state of the API response, while allowing a nicer serialization

>>> print(cable.serialize())

 {'a_terminations': [{'object_id': 530082, 'object_type': 'dcim.interface'}],
 'b_terminations': [{'object_id': 314260, 'object_type': 'dcim.frontport'}],
 'color': '',
 'comments': '',
 'created': '2022-05-03T02:00:00+02:00',
 'custom_fields': {},
 'description': '',
 'display': '1001',
 'id': 155652,
 'label': '1001',
 'last_updated': '2022-06-29T14:36:49.386886+02:00',
 'length': None,
 'length_unit': None,
 'status': 'connected',
 'tags': [],
 'tenant': 2510,
 'type': '',
 'url': 'https://localhost:8000/api/dcim/cables/155652/'}

The __getattr__ on the new class, allows to maintain backward compatibility to some extent.

>>> print(cables.a_terminations[0].label)
'ETH-1'

This involves a change in type of objects returned in mixed/generic lists now always being GenericListItem

@srfwx srfwx changed the title Draft: 555 fix cable serialize 555 fix cable serialize Dec 9, 2024
@srfwx srfwx changed the title 555 fix cable serialize Fix cable serialize Dec 9, 2024
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

Successfully merging this pull request may close these issues.

Cables.serialize() is inconsistent
1 participant