nautobot / pynautobot

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

Error when updating permission object including constraints #27

Closed david-kn closed 2 years ago

david-kn commented 2 years ago

Environment

Hi, I found out that when updating a permission object (not a problem when creating the identical object for the first time) containing constraints with a specific JSON string (a list of objects), pynautobot would fail with an exception Exception has occurred: AttributeError object has no attribute "id"

I tried to analyze the issue and it seems that the error is generated within _diff() -> serialize() method because it would treat JSON string as a Recordobject and expecting id attribute within (which of course cannot be present).

Steps to Reproduce

  1. Let's have a permission object within nautobot
  2. Try to update such an object via pynautobot while having constraints attr containing a "list", e.g. [{"_custom_field_data__owner": "constraint-string-value"}] (valid JSON array of object(s))
  3. Update via pyanutobot fails.

Expected Behavior

Just save the object including constraints.

If you send the same data via a cURL query, it passes and saves without an issue. Example:

curl --location --request PATCH 'https://server/api/users/permissions/cadcf757-da95-46e2-9ef7-d3a08e6115f9/' \ --header 'accept: application/json' \ --header 'Content-Type: application/json' \ --header 'Authorization: Token 123456789' \ --data-raw '{"constraints": [{"_custom_field_data__owner": "constraint-string-value"}]}'

I am enclosing a PR (https://github.com/nautobot/pynautobot/pull/28) that solves this issue hopefully at the right spot, but of course I don't see into all the depths of nautobot and other relations between models and stuff :-)

jvanderaa commented 2 years ago

Closing with the release of 1.04. Thank you @david-kn

https://pypi.org/project/pynautobot/