Skip to content

Unable to nullify choice fields via the API #4083

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

Closed
fantasmita opened this issue Feb 4, 2020 · 6 comments · Fixed by #4133
Closed

Unable to nullify choice fields via the API #4083

fantasmita opened this issue Feb 4, 2020 · 6 comments · Fixed by #4133
Assignees
Labels
status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@fantasmita
Copy link

Environment

  • Python version: 3.6.5
  • NetBox version: 2.7.3

To fully unrack a device, you must blank out its rack, position, and face values.
The face field has a non-null constraint and has an API validator that requires its value to be either front or rear, so it's not possible to blank out the value through the API which prevents the device from being removed from the rack.

Steps to Reproduce

  1. Create a device in netbox with values for rack, position, and face.
  2. Try to clear all three values with a PATCH like the following
curl -X PATCH "https://$NETBOX/api/dcim/devices/$ID/" -H  "accept: application/json" -H  "Content-Type: application/json" -H  "X-CSRFToken: $TOKEN" -d "{  \"rack\": null,  \"position\": null,  \"face\": \"\"}"

Expected Behavior

The device to be unracked

Observed Behavior

{
  "face": [
    " is not a valid choice."
  ]
}

If passing null for the face value, the django non-null constraint is hit.
If patching just the rack and position values without touching the face, the Cannot select a rack face without assigning a rack. validator is hit.

@jeremystretch
Copy link
Member

jeremystretch commented Feb 4, 2020

Omit the face field if you are unracking a device: It will be nullified automatically. The following request is successful:

curl -X PATCH http://localhost:8000/api/dcim/devices/<ID>/ \
-H "Authorization: Token <TOKEN>" \
-H "Content-Type: application/json" \
-H "Accept: application/json; indent=4" \
-d '{"rack": null, "position": null}'

@fantasmita
Copy link
Author

Thanks for looking and for the suggestions.
However, I don't think the nullification is working as expected.

I know netboxdemo.com is unsanctioned, but here's a quick reproducible illustration of the problem in that environment when following your suggestions.

setting face to the empty string:

$ curl -X PATCH "https://netboxdemo.com/api/dcim/devices/1/" \
>     -H "Authorization: Token  72830d67beff4ae178b94d8f781842408df8069d" \
>     -H "Content-Type: application/json" \
>     -H "Accept: application/json; indent=4" \
>     -d '{"rack": null, "position": null, "face": ""}'
{
    "face": [
        " is not a valid choice."
    ]
}

omitting face:

$ curl -X PATCH "https://netboxdemo.com/api/dcim/devices/1/" \
>     -H "Authorization: Token  72830d67beff4ae178b94d8f781842408df8069d" \
>     -H "Content-Type: application/json" \
>     -H "Accept: application/json; indent=4" \
>     -d '{"rack": null, "position": null}'
{
    "face": [
        "Cannot select a rack face without assigning a rack."
    ]
}

@jeremystretch
Copy link
Member

Sorry, I had pasted the wrong command in my previous comment. I've corrected it to omit the face field. This is working on v2.7.4.

@fantasmita
Copy link
Author

No worries, thanks.

I'm sorry for not explaining myself more clearly, but omitting the face when making a PATCH request doesn't successfully nullify the value before it hits the site / rack validators, which results in the Cannot select a rack face without assigning a rack. validation error I pasted above.
I confirmed today that this still occurs on a fresh installation of v2.7.4.

Anyway, I'm happy to work around this and it's definitely possible I'm missing something, but let me know if you have other thoughts or if I can provide any other information. Thanks again for your time

@jeremystretch jeremystretch reopened this Feb 5, 2020
@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application labels Feb 10, 2020
@jeremystretch
Copy link
Member

Sorry, this took a while to track down. I wasn't able to replicate the error earlier because the device that I was testing against did not have a rack face already assigned, so the validation check was not being triggered.

@jeremystretch jeremystretch self-assigned this Feb 10, 2020
@jeremystretch jeremystretch changed the title unable to unrack devices through API Unable to nullify choice fields via the API Feb 10, 2020
jeremystretch added a commit that referenced this issue Feb 10, 2020
…hoices

Fixes #4083: Permit nullifying applicable choice fields via API requests
@fantasmita
Copy link
Author

Great, thanks a bunch!

@lock lock bot locked as resolved and limited conversation to collaborators May 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants