nautobot / pynautobot

Nautobot Python SDK
https://pynautobot.readthedocs.io/en/latest/index.html
Apache License 2.0
38 stars 32 forks source link

JSON CustomField Type unable to serialize Dictionary #71

Closed whitej6 closed 2 years ago

whitej6 commented 2 years ago

Nautobot: 1.3.9 -1.4.1 pynautobot: 1.1.2 Python: 3.8

Observed Behavior:

Expected Behavior:

whitej6 commented 2 years ago

Example with dictionary

>>> device = nautobot.dcim.devices.all()[0]
>>> device.custom_fields
{'json_test': {'bar': 'baz', 'foo': 1}}
>>> device.serialize()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/whitej6/Desktop/.venv/lib/python3.9/site-packages/pynautobot/core/response.py", line 335, in serialize
    ret[i] = flatten_custom(current_val)
  File "/Users/whitej6/Desktop/.venv/lib/python3.9/site-packages/pynautobot/core/response.py", line 63, in flatten_custom
    return {k: v if not isinstance(v, dict) else v["value"] for k, v in custom_dict.items()}
  File "/Users/whitej6/Desktop/.venv/lib/python3.9/site-packages/pynautobot/core/response.py", line 63, in <dictcomp>
    return {k: v if not isinstance(v, dict) else v["value"] for k, v in custom_dict.items()}
KeyError: 'value'

Example with list

>>> device = nautobot.dcim.devices.all()[1]
>>> device.custom_fields
{'json_test': [1, 2, 3]}
>>> device.serialize()
{'id': '7cc62d17-8b67-41fc-9233-03f10b9b3c49', 'display': 'HOU-WAN00', 'url': 'https://nautobot.whitej6.com/api/dcim/devices/7cc62d17-8b67-41fc-9233-03f10b9b3c49/', 'name': 'HOU-WAN00', 'device_type': '5795540f-e9c1-492a-bbc5-46d001c93207', 'device_role': '637f1db1-1656-4ebd-813c-0cf0f22d1747', 'tenant': None, 'platform': 'cceb8e56-1e1f-4b32-a4c3-1fa26b9681bb', 'serial': '', 'asset_tag': None, 'site': 'c09f95cf-d37e-4fec-8312-06e8ff340011', 'location': None, 'rack': None, 'position': None, 'face': None, 'parent_device': None, 'status': 'active', 'primary_ip': None, 'primary_ip4': None, 'primary_ip6': None, 'secrets_group': None, 'cluster': None, 'virtual_chassis': None, 'vc_position': None, 'vc_priority': None, 'comments': '', 'local_context_schema': None, 'local_context_data': None, 'config_context': {}, 'created': '2022-08-29', 'last_updated': '2022-08-29T18:30:19.674675Z', 'tags': [], 'notes_url': 'https://nautobot.whitej6.com/api/dcim/devices/7cc62d17-8b67-41fc-9233-03f10b9b3c49/notes/', 'custom_fields': {'json_test': [1, 2, 3]}}
>>> 
whitej6 commented 2 years ago

https://github.com/nautobot/pynautobot/blob/develop/pynautobot/core/response.py#L62 Offending code

whitej6 commented 2 years ago

Resolved by #72